<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="这是一篇关于MYSQL基础语法（下）">
<meta property="og:type" content="article">
<meta property="og:title" content="MYSQL基础语法（下）">
<meta property="og:url" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="这是一篇关于MYSQL基础语法（下）">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180102209.bmp">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180103700.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180103403.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE03.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE05.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE06.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE07.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E9%BB%98%E8%AE%A4%E5%80%BC.png">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/1.bmp">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E5%A4%9A%E5%AF%B9%E5%A4%9A%E5%85%B3%E7%B3%BB%E5%9B%BE%E8%A7%A3.PNG">
<meta property="og:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/2.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/3.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/4.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/5.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/6.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/7.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/8.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/9.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/10.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/11.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/12.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/12.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E8%A1%A8%E4%B9%8B%E9%97%B4%E7%9A%84%E4%B8%80%E5%AF%B9%E5%A4%9A%E5%85%B3%E7%B3%BB.PNG">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%B8%80%E5%AF%B9%E4%B8%80%E8%A1%A8%E8%AE%BE%E8%AE%A1%E5%9B%BE%E8%A7%A3.PNG">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E8%AE%BE%E8%AE%A1%E7%BB%83%E4%B9%A0.PNG">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E8%AE%BE%E8%AE%A1%E7%BB%83%E4%B9%A0%E6%9C%80%E5%90%8E%E7%BB%93%E6%9E%9C.PNG">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1%E5%AF%B9%E5%A4%9A%E7%BB%83%E4%B9%A001.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E5%AF%B9%E5%A4%9A%E7%BB%83%E4%B9%A001.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A21.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1550291804592.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E9%97%AE%E9%A2%98.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E9%97%AE%E9%A2%98%E5%9B%BE%E8%A7%A3.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E6%98%BE%E7%A4%BA%E5%86%85%E8%BF%9E%E6%8E%A5%E8%A7%A3%E5%86%B3%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E9%97%AE%E9%A2%98.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%86%85%E8%BF%9E%E6%8E%A5%E7%BB%93%E6%9E%9C.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E6%98%BE%E7%A4%BA%E5%86%85%E8%BF%9E%E6%8E%A5%E8%A7%A3%E5%86%B3%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E9%97%AE%E9%A2%982.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%81%9A%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%8E%9F%E7%90%86%E5%9B%BE.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%B7%A6%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%9B%BE%E8%A7%A3.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%B7%A6%E5%A4%96%E8%BF%9E%E6%8E%A5%E7%BB%93%E6%9E%9C2.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%8E%9F%E7%90%86.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%AE%9E%E7%8E%B0.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%AE%9E%E7%8E%B02.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5%E5%92%8C%E5%B7%A6%E5%A4%96%E8%BF%9E%E6%8E%A5%E4%BA%92%E6%8D%A2.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20200802103333568.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A201-16418728627231.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1562225585954.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1562225605676.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1562225632007.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A205-16418728627242.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A206-16418728627243.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A207-16418728627244.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A208-16418728627247.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A209-16418728627245.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A210-16418728627246.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A211-16418728627248.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A212-16418728627249.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A214-164187286272410.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A213-164187286272411.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E7%BB%83%E4%B9%A0%E5%9B%BE%E8%A7%A31.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%BB%83%E4%B9%A0.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%BB%83%E4%B9%A02.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%BB%83%E4%B9%A03.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20200727092438331.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A201-1597203787779.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20200727093014584.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A202-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A203-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A204-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A205-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20200727093902316.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A206-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A207-1597203787780.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A208-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A209-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A210-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20200727204121609.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A211-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A212-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A213-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A214-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A215-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A211-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A212-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A213-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A214-1597203787781.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A216-1597203787782.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A217-1597203787782.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A218-1597203787782.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A219-1597203787784.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A220-1597203787784.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A221-1597203787784.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A222-1597203787784.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/1563360456604.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/mysql%E4%BA%8B%E5%8A%A11.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A101.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%9B%9E%E6%BB%9A%E4%BA%8B%E5%8A%A1.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E8%87%AA%E5%8A%A8%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A119.png">
<meta property="og:image" content="http://ethanliu6.github.io/Users/ethan.liu/Desktop/02_CodeData/Code#016%2003-%E6%95%B0%E6%8D%AE%E5%BA%93%E9%98%B6%E6%AE%B5%E8%AF%BE%E4%BB%B6/%E6%95%B0%E6%8D%AE%E5%BA%93_day02/01_%E8%AF%BE%E4%BB%B6/%E6%8E%88%E8%AF%BE%E7%AC%94%E8%AE%B0/img/%E8%84%8F%E8%AF%BB.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%B8%8D%E5%8F%AF%E9%87%8D%E5%A4%8D%E8%AF%BB.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E5%B9%BB%E8%AF%BB.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A127.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A128.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/image-20210717105819743.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A1290.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/294.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/293.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/295.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A132.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/296.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A134.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A135.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A136.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A137.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/297.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/298.bmp">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A140.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/img/%E4%BA%8B%E5%8A%A141.png">
<meta property="article:published_time" content="2024-04-30T16:00:00.000Z">
<meta property="article:modified_time" content="2024-05-17T17:13:03.367Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="MySQL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180102209.bmp">


<link rel="canonical" href="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/","path":"2024/05/01/JavaNotes/MySQL/day02/day02-MySQL高级笔记/","title":"MYSQL基础语法（下）"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>MYSQL基础语法（下） | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%9B%9E%E9%A1%BE"><span class="nav-number">1.</span> <span class="nav-text">回顾</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#mysql%E7%AE%A1%E7%90%86%E6%95%B0%E6%8D%AE%E5%BA%93"><span class="nav-number">1.1.</span> <span class="nav-text">MySQL管理数据库</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A8%E7%9A%84%E7%AE%A1%E7%90%86"><span class="nav-number">1.2.</span> <span class="nav-text">表的管理</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9F%A5%E7%9C%8B%E4%B8%80%E4%B8%AA%E8%A1%A8%E7%BB%93%E6%9E%84"><span class="nav-number">1.3.</span> <span class="nav-text">查看一个表结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BF%AE%E6%94%B9%E8%A1%A8%E7%BB%93%E6%9E%84"><span class="nav-number">1.4.</span> <span class="nav-text">修改表结构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E8%A1%A8"><span class="nav-number">1.5.</span> <span class="nav-text">删除表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AE%A1%E7%90%86%E6%95%B0%E6%8D%AE%E6%95%B0%E6%8D%AE%E5%A2%9E%E5%88%A0%E6%94%B9dml"><span class="nav-number">1.6.</span> <span class="nav-text">管理数据：数据增删改DML</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%8A%E6%97%A5%E9%87%8D%E7%82%B9"><span class="nav-number">2.</span> <span class="nav-text">今日重点</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC%E4%B8%80%E9%83%A8%E5%88%86%E7%BA%A6%E6%9D%9F%E5%92%8C%E8%A1%A8%E8%AE%BE%E8%AE%A1"><span class="nav-number">3.</span> <span class="nav-text">第一部分(约束和表设计)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#dql%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5-limit%E8%AF%AD%E5%8F%A5%E6%8E%8C%E6%8F%A1"><span class="nav-number">3.1.</span> <span class="nav-text">1、DQL查询语句-limit语句(掌握)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87"><span class="nav-number">3.1.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3"><span class="nav-number">3.1.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93"><span class="nav-number">3.1.3.</span> <span class="nav-text">小结</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-1"><span class="nav-number">3.1.4.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BB%E9%94%AE%E8%87%AA%E5%A2%9E"><span class="nav-number">3.2.</span> <span class="nav-text">4、主键自增</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-1"><span class="nav-number">3.2.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-1"><span class="nav-number">3.2.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-2"><span class="nav-number">3.2.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%94%AF%E4%B8%80%E7%BA%A6%E6%9D%9F"><span class="nav-number">3.3.</span> <span class="nav-text">5、唯一约束</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-2"><span class="nav-number">3.3.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-2"><span class="nav-number">3.3.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%94%AF%E4%B8%80%E7%BA%A6%E6%9D%9F%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">3.3.2.1.</span> <span class="nav-text">唯一约束的作用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%94%AF%E4%B8%80%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%9F%BA%E6%9C%AC%E6%A0%BC%E5%BC%8F"><span class="nav-number">3.3.2.2.</span> <span class="nav-text">唯一约束的基本格式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-3"><span class="nav-number">3.3.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E7%A9%BA%E7%BA%A6%E6%9D%9F"><span class="nav-number">3.4.</span> <span class="nav-text">6、非空约束</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-3"><span class="nav-number">3.4.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-3"><span class="nav-number">3.4.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9D%9E%E7%A9%BA%E7%BA%A6%E6%9D%9F%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">3.4.2.1.</span> <span class="nav-text">非空约束的作用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9D%9E%E7%A9%BA%E7%BA%A6%E6%9D%9F%E7%9A%84%E5%9F%BA%E6%9C%AC%E8%AF%AD%E6%B3%95%E6%A0%BC%E5%BC%8F"><span class="nav-number">3.4.2.2.</span> <span class="nav-text">非空约束的基本语法格式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-4"><span class="nav-number">3.4.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E5%80%BC%E4%BA%86%E8%A7%A3"><span class="nav-number">3.5.</span> <span class="nav-text">7、默认值(了解)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-4"><span class="nav-number">3.5.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-4"><span class="nav-number">3.5.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E5%80%BC%E7%9A%84%E4%BD%9C%E7%94%A8"><span class="nav-number">3.5.2.1.</span> <span class="nav-text">默认值的作用</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E5%80%BC%E6%A0%BC%E5%BC%8F"><span class="nav-number">3.5.2.2.</span> <span class="nav-text">默认值格式</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-5"><span class="nav-number">3.5.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A8%E5%85%B3%E7%B3%BB%E7%9A%84%E6%A6%82%E5%BF%B5%E5%92%8C%E5%A4%96%E9%94%AE%E7%BA%A6%E6%9D%9F"><span class="nav-number">3.6.</span> <span class="nav-text">8、表关系的概念和外键约束</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E5%AF%B9%E5%A4%9A%E6%8E%8C%E6%8F%A1"><span class="nav-number">3.6.1.</span> <span class="nav-text">多对多(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%96%E9%94%AE%E7%BA%A6%E6%9D%9F%E6%8E%8C%E6%8F%A1"><span class="nav-number">3.6.2.</span> <span class="nav-text">外键约束(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%96%E9%94%AE%E7%9A%84%E7%BA%A7%E8%81%94%E6%8E%8C%E6%8F%A1"><span class="nav-number">3.6.3.</span> <span class="nav-text">外键的级联(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E5%AF%B9%E5%A4%9A%E6%8E%8C%E6%8F%A1"><span class="nav-number">3.6.4.</span> <span class="nav-text">一对多(掌握)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E5%AF%B9%E4%B8%80"><span class="nav-number">3.6.5.</span> <span class="nav-text">一对一</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%A1%A8%E8%AE%BE%E8%AE%A1%E6%A1%88%E4%BE%8B%E7%BB%83%E4%B9%A0"><span class="nav-number">3.7.</span> <span class="nav-text">9、表设计案例(练习)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-5"><span class="nav-number">3.7.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-5"><span class="nav-number">3.7.2.</span> <span class="nav-text">讲解</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BD%9C%E4%B8%9A%E5%A4%9A%E8%A1%A8%E7%BB%83%E4%B9%A0"><span class="nav-number">3.8.</span> <span class="nav-text">作业:多表练习</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E5%AF%B9%E5%A4%9A%E5%85%B3%E7%B3%BB%E7%BB%83%E4%B9%A0"><span class="nav-number">3.8.1.</span> <span class="nav-text">一对多关系练习</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A4%9A%E5%AF%B9%E5%A4%9A%E5%85%B3%E7%B3%BB%E7%BB%83%E4%B9%A0"><span class="nav-number">3.8.2.</span> <span class="nav-text">多对多关系练习</span></a></li></ol></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC%E4%BA%8C%E9%83%A8%E5%88%86%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.</span> <span class="nav-text">第二部分(多表查询)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E4%BB%8B%E7%BB%8D%E6%8E%8C%E6%8F%A1"><span class="nav-number">4.1.</span> <span class="nav-text">1、多表查询介绍(掌握)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-6"><span class="nav-number">4.1.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-6"><span class="nav-number">4.1.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.1.2.1.</span> <span class="nav-text">什么是多表查询</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E7%9A%84%E5%88%86%E7%B1%BB"><span class="nav-number">4.1.2.2.</span> <span class="nav-text">多表查询的分类</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-6"><span class="nav-number">4.1.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E7%8E%B0%E8%B1%A1"><span class="nav-number">4.2.</span> <span class="nav-text">2、笛卡尔积现象</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-7"><span class="nav-number">4.2.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-7"><span class="nav-number">4.2.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE"><span class="nav-number">4.2.2.1.</span> <span class="nav-text">准备数据</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E7%8E%B0%E8%B1%A1"><span class="nav-number">4.2.2.2.</span> <span class="nav-text">什么是笛卡尔积现象</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E6%B8%85%E9%99%A4%E7%AC%9B%E5%8D%A1%E5%B0%94%E7%A7%AF%E7%8E%B0%E8%B1%A1%E7%9A%84%E5%BD%B1%E5%93%8D"><span class="nav-number">4.2.2.3.</span> <span class="nav-text">如何清除笛卡尔积现象的影响</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-7"><span class="nav-number">4.2.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%86%85%E8%BF%9E%E6%8E%A5"><span class="nav-number">4.3.</span> <span class="nav-text">3、内连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-8"><span class="nav-number">4.3.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-8"><span class="nav-number">4.3.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%86%85%E8%BF%9E%E6%8E%A5"><span class="nav-number">4.3.2.1.</span> <span class="nav-text">什么是内连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%9A%90%E5%BC%8F%E5%86%85%E8%BF%9E%E6%8E%A5"><span class="nav-number">4.3.2.2.</span> <span class="nav-text">隐式内连接</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%98%BE%E7%A4%BA%E5%86%85%E8%BF%9E%E6%8E%A5%E6%8E%8C%E6%8F%A1"><span class="nav-number">4.3.2.3.</span> <span class="nav-text">显示内连接(掌握)</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-8"><span class="nav-number">4.3.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B7%A6%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="nav-number">4.4.</span> <span class="nav-text">4、左外连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-9"><span class="nav-number">4.4.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-9"><span class="nav-number">4.4.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-9"><span class="nav-number">4.4.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8F%B3%E5%A4%96%E8%BF%9E%E6%8E%A5"><span class="nav-number">4.5.</span> <span class="nav-text">5、右外连接</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-10"><span class="nav-number">4.5.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-10"><span class="nav-number">4.5.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-10"><span class="nav-number">4.5.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.6.</span> <span class="nav-text">6、子查询</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-11"><span class="nav-number">4.6.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-11"><span class="nav-number">4.6.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.6.2.1.</span> <span class="nav-text">什么是子查询</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E4%B8%80%E6%9D%A1%E6%9F%A5%E8%AF%A2%E8%AF%AD%E5%8F%A5%E7%BB%93%E6%9E%9C%E4%BD%9C%E4%B8%BA%E5%8F%A6%E4%B8%80%E6%9D%A1%E6%9F%A5%E8%AF%A2%E8%AF%AD%E6%B3%95%E4%B8%80%E9%83%A8%E5%88%86"><span class="nav-number">4.6.2.1.1.</span> <span class="nav-text">一条查询语句结果作为另一条查询语法一部分。</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E7%9A%84%E4%B8%89%E7%A7%8D%E6%83%85%E5%86%B5"><span class="nav-number">4.6.2.2.</span> <span class="nav-text">子查询结果的三种情况</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-11"><span class="nav-number">4.6.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E7%BB%93%E6%9E%9C%E6%98%AF%E5%8D%95%E8%A1%8C%E5%8D%95%E5%88%97%E7%9A%84%E6%97%B6%E5%80%99"><span class="nav-number">4.7.</span> <span class="nav-text">7、子查询的结果是单行单列的时候</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-12"><span class="nav-number">4.7.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-12"><span class="nav-number">4.7.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-12"><span class="nav-number">4.7.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%BB%93%E6%9E%9C%E6%98%AF%E5%A4%9A%E8%A1%8C%E5%8D%95%E5%88%97%E7%9A%84%E6%97%B6%E5%80%99"><span class="nav-number">4.8.</span> <span class="nav-text">8、子查询结果是多行单列的时候</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-13"><span class="nav-number">4.8.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-13"><span class="nav-number">4.8.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-13"><span class="nav-number">4.8.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%90%E6%9F%A5%E8%AF%A2%E7%9A%84%E7%BB%93%E6%9E%9C%E6%98%AF%E5%A4%9A%E8%A1%8C%E5%A4%9A%E5%88%97"><span class="nav-number">4.9.</span> <span class="nav-text">9、子查询的结果是多行多列</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-14"><span class="nav-number">4.9.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-14"><span class="nav-number">4.9.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-14"><span class="nav-number">4.9.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E6%A1%88%E4%BE%8B%E5%AD%90%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.10.</span> <span class="nav-text">10、多表查询案例(子查询)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE-1"><span class="nav-number">4.10.1.</span> <span class="nav-text">准备数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A01"><span class="nav-number">4.10.2.</span> <span class="nav-text">练习1</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-15"><span class="nav-number">4.10.2.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-15"><span class="nav-number">4.10.2.2.</span> <span class="nav-text">讲解</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A02"><span class="nav-number">4.10.3.</span> <span class="nav-text">练习2</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-16"><span class="nav-number">4.10.3.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-16"><span class="nav-number">4.10.3.2.</span> <span class="nav-text">讲解</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A03"><span class="nav-number">4.10.4.</span> <span class="nav-text">练习3</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-17"><span class="nav-number">4.10.4.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-17"><span class="nav-number">4.10.4.2.</span> <span class="nav-text">讲解</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A04"><span class="nav-number">4.10.5.</span> <span class="nav-text">练习4</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-18"><span class="nav-number">4.10.5.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-18"><span class="nav-number">4.10.5.2.</span> <span class="nav-text">讲解</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E6%A1%88%E4%BE%8B%E8%BF%9E%E6%8E%A5%E6%9F%A5%E8%AF%A2"><span class="nav-number">4.11.</span> <span class="nav-text">11、 多表查询案例(连接查询)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%87%86%E5%A4%87%E6%95%B0%E6%8D%AE-2"><span class="nav-number">4.11.1.</span> <span class="nav-text">1.准备数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%83%E4%B9%A0"><span class="nav-number">4.11.2.</span> <span class="nav-text">2 练习</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A01-1"><span class="nav-number">4.11.2.1.</span> <span class="nav-text">1.1 练习1</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A02-1"><span class="nav-number">4.11.2.2.</span> <span class="nav-text">1.2 练习2</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A03-1"><span class="nav-number">4.11.2.3.</span> <span class="nav-text">1.3 练习3</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#%E5%A4%9A%E8%A1%A8%E6%9F%A5%E8%AF%A2%E8%A7%84%E5%BE%8B%E6%80%BB%E7%BB%93"><span class="nav-number">4.11.2.3.1.</span> <span class="nav-text">1.3.1 多表查询规律总结</span></a></li></ol></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A04-1"><span class="nav-number">4.11.2.4.</span> <span class="nav-text">1.4 练习4</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%83%E4%B9%A05"><span class="nav-number">4.11.2.5.</span> <span class="nav-text">1.5 练习5</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%AD%A6%E4%B9%A0%E6%80%BB%E7%BB%93"><span class="nav-number">4.12.</span> <span class="nav-text">学习总结</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AC%AC%E4%B8%89%E9%83%A8%E5%88%86%E4%BA%8B%E5%8A%A1"><span class="nav-number">5.</span> <span class="nav-text">第三部分(事务)</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E7%9A%84%E6%A6%82%E5%BF%B5"><span class="nav-number">5.1.</span> <span class="nav-text">1、事务的概念</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-19"><span class="nav-number">5.1.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-19"><span class="nav-number">5.1.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E7%9A%84%E5%BA%94%E7%94%A8%E5%9C%BA%E6%99%AF%E8%AF%B4%E6%98%8E"><span class="nav-number">5.1.2.1.</span> <span class="nav-text">事务的应用场景说明</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E4%BA%8B%E5%8A%A1"><span class="nav-number">5.1.2.2.</span> <span class="nav-text">什么是事务</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-15"><span class="nav-number">5.1.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%89%8B%E5%8A%A8%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1"><span class="nav-number">5.2.</span> <span class="nav-text">2、手动提交事务</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-20"><span class="nav-number">5.2.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-20"><span class="nav-number">5.2.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%89%8B%E5%8A%A8%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1%E4%BD%BF%E7%94%A8%E6%AD%A5%E9%AA%A4"><span class="nav-number">5.2.2.1.</span> <span class="nav-text">手动提交事务使用步骤</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-16"><span class="nav-number">5.2.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%87%AA%E5%8A%A8%E6%8F%90%E4%BA%A4%E4%BA%8B%E5%8A%A1"><span class="nav-number">5.3.</span> <span class="nav-text">3、自动提交事务</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-21"><span class="nav-number">5.3.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-21"><span class="nav-number">5.3.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-17"><span class="nav-number">5.3.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%8E%9F%E7%90%86"><span class="nav-number">5.4.</span> <span class="nav-text">4、原理</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8E%9F%E7%90%86%E8%AF%B4%E6%98%8E"><span class="nav-number">5.4.1.</span> <span class="nav-text">原理说明</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E6%93%8D%E4%BD%9C%E5%B0%8F%E7%BB%93"><span class="nav-number">5.4.2.</span> <span class="nav-text">事务操作小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E7%9A%84%E5%9B%9B%E5%A4%A7%E7%89%B9%E6%80%A7acid%E9%9D%A2%E8%AF%95"><span class="nav-number">5.5.</span> <span class="nav-text">5、事务的四大特性(ACID)(面试)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-22"><span class="nav-number">5.5.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-22"><span class="nav-number">5.5.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-18"><span class="nav-number">5.5.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E7%9A%84%E5%B9%B6%E5%8F%91%E8%AE%BF%E9%97%AE%E5%BC%95%E5%8F%91%E7%9A%84%E4%B8%89%E4%B8%AA%E9%97%AE%E9%A2%98%E9%9D%A2%E8%AF%95"><span class="nav-number">5.6.</span> <span class="nav-text">6、事务的并发访问引发的三个问题(面试)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-23"><span class="nav-number">5.6.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-23"><span class="nav-number">5.6.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-19"><span class="nav-number">5.6.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E5%8A%A1%E7%9A%84%E9%9A%94%E7%A6%BB%E7%BA%A7%E5%88%AB"><span class="nav-number">5.7.</span> <span class="nav-text">7、事务的隔离级别</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-24"><span class="nav-number">5.7.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-24"><span class="nav-number">5.7.2.</span> <span class="nav-text">讲解</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-20"><span class="nav-number">5.7.3.</span> <span class="nav-text">小结</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%84%8F%E8%AF%BB%E7%9A%84%E6%BC%94%E7%A4%BA%E8%AF%BE%E4%B8%8B%E4%B8%8D%E7%94%A8%E6%BC%94%E7%A4%BA"><span class="nav-number">5.8.</span> <span class="nav-text">8、脏读的演示(课下不用演示)</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%9B%AE%E6%A0%87-25"><span class="nav-number">5.8.1.</span> <span class="nav-text">目标</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%AE%B2%E8%A7%A3-25"><span class="nav-number">5.8.2.</span> <span class="nav-text">讲解</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9F%A5%E8%AF%A2%E5%92%8C%E8%AE%BE%E7%BD%AE%E9%9A%94%E7%A6%BB%E7%BA%A7%E5%88%AB"><span class="nav-number">5.8.2.1.</span> <span class="nav-text">查询和设置隔离级别</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%84%8F%E8%AF%BB%E7%9A%84%E6%BC%94%E7%A4%BA"><span class="nav-number">5.8.2.2.</span> <span class="nav-text">脏读的演示</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B0%8F%E7%BB%93-21"><span class="nav-number">5.8.3.</span> <span class="nav-text">小结</span></a></li></ol></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="MYSQL基础语法（下） | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          MYSQL基础语法（下）<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvTXlTUUwvZGF5MDIvZGF5MDItTXlTUUzpq5jnuqfnrJTorrAubWQ=" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-05-01 00:00:00" itemprop="dateCreated datePublished" datetime="2024-05-01T00:00:00+08:00">2024-05-01</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-05-18 01:13:03" itemprop="dateModified" datetime="2024-05-18T01:13:03+08:00">2024-05-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/MySQL/" itemprop="url" rel="index"><span itemprop="name">MySQL</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="MYSQL基础语法（下）" href="/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::efb054fbd33f314ba5060558e5ac1b0a" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>13k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>48 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>这是一篇关于MYSQL基础语法（下）</p>

</blockquote>
<span id="more"></span>
<h1 id="回顾">回顾</h1>
<h2 id="mysql管理数据库">MySQL管理数据库</h2>
<p>创建数据库</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CREATE DATABASE IF NOT EXISTS 数据库名;</span><br></pre></td></tr></table></figure>
<p>删除数据库</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DROP DATABASE 数据库名;</span><br></pre></td></tr></table></figure>
<h2 id="表的管理">表的管理</h2>
<p>查看所有表</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">use 数据库名; 选中一个数据库</span><br><span class="line">show tables;</span><br></pre></td></tr></table></figure>
<p>创建表:student(整数id,字符串name,整数age)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">CREATE TABLE sutdent (id int, name varchar(20), age int);</span><br></pre></td></tr></table></figure>
<h2 id="查看一个表结构">查看一个表结构</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">desc 表名;</span><br></pre></td></tr></table></figure>
<p>查看创建表的sql语句</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">SHOW CREATE TABLE 表名;</span><br></pre></td></tr></table></figure>
<h2 id="修改表结构">修改表结构</h2>
<p>ALTER TABLE 表名 ...</p>
<p>向学生表中添加1列remark varchar(100)</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ALTER TABLE student ADD remark varchar(100)</span><br></pre></td></tr></table></figure>
<p>删除学生表列remark</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">ALTER TABLE student DROP remark;</span><br></pre></td></tr></table></figure>
<p>修改表student名字为student1</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">REANME TABLE sutdent TO student1;</span><br></pre></td></tr></table></figure>
<h2 id="删除表">删除表</h2>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DROP TABLE 表名;</span><br></pre></td></tr></table></figure>
<h2 id="管理数据数据增删改dml">管理数据：数据增删改DML</h2>
<p>插入数据</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">INSERT INTO 表名 (字段名1, 字段名2..) VALUES (值1, 值2...);</span><br></pre></td></tr></table></figure>
<p>修改数据</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">UPDATE 表名 SET 字段名=新的值 WHERE 条件;</span><br></pre></td></tr></table></figure>
<p>删除表中的所有数据</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">DELETE FROM 表名;</span><br></pre></td></tr></table></figure>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>排序：</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名 <span class="keyword">order</span> <span class="keyword">by</span> 字段名 <span class="keyword">asc</span>(升序默认的)<span class="operator">/</span><span class="keyword">desc</span>(降序),字段名 <span class="keyword">asc</span>(升序默认的)<span class="operator">/</span><span class="keyword">desc</span>(降序),..</span><br><span class="line"><span class="number">2.</span>聚合函数：</span><br><span class="line"><span class="built_in">count</span>(<span class="operator">*</span>) 根据列统计行数</span><br><span class="line"><span class="built_in">sum</span>(字段名)求某一列和值</span><br><span class="line"><span class="built_in">max</span>(字段名)某一列最大值</span><br><span class="line"><span class="built_in">min</span>(字段名)某一列最小值</span><br><span class="line"><span class="built_in">avg</span>(字段名)某一列平均值</span><br><span class="line"><span class="number">3.</span>分组：按照哪列分组，那么那一列的字段值相同就被分为一组</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名 <span class="keyword">where</span> 条件 <span class="keyword">group</span> <span class="keyword">by</span> 字段名 <span class="keyword">having</span> 条件</span><br><span class="line"><span class="number">4.</span>分页查询</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名 limit 起始索引,每页显示的行数;</span><br><span class="line">	起始索引 从<span class="number">0</span>开始，对应第一行</span><br><span class="line"><span class="number">5.</span>约束：</span><br><span class="line">	<span class="number">1</span>）主键约束：字段名 类型 <span class="keyword">primary</span> key auto_increment</span><br><span class="line">		特点：唯一  非空</span><br><span class="line">	<span class="number">2</span>）唯一约束：字段名 类型 <span class="keyword">unique</span></span><br><span class="line">	<span class="number">3</span>)非空约束：字段名 类型 <span class="keyword">not</span> <span class="keyword">null</span></span><br><span class="line">	<span class="number">4</span>)默认值约束：字段名 类型 <span class="keyword">default</span> 默认值 </span><br><span class="line">	<span class="number">5</span>）外键约束：约束外键，在多表中具有外键。</span><br><span class="line">		【<span class="keyword">constraint</span> 外键约束名】 <span class="keyword">foreign</span> key(外键对应的字段名) <span class="keyword">references</span> 主表(主键);</span><br><span class="line">	<span class="number">6</span>）多表关系：</span><br><span class="line">		<span class="number">1</span>）一对多：将一方的主键作为多方的外键</span><br><span class="line">		<span class="number">2</span>）多对多：创建从表(中间表)维护两张主表的关系，在从表中至少要有两列来自于主表的主键作为外键</span><br><span class="line">		<span class="number">3</span>）一对一：将任意一方的主键作为另一方的外键</span><br><span class="line">		</span><br></pre></td></tr></table></figure>
<h1 id="今日重点">今日重点</h1>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span>约束</span><br><span class="line"><span class="number">2.</span>多表查询(最为重要)</span><br><span class="line"><span class="number">3.</span>事务(mysql如何控制事务)</span><br></pre></td></tr></table></figure>
<h1 id="第一部分约束和表设计">第一部分(约束和表设计)</h1>
<h2 id="dql查询语句-limit语句掌握">1、DQL查询语句-limit语句(掌握)</h2>
<h3 id="目标">目标</h3>
<p>能够掌握limit语句的使用</p>
<h3 id="讲解">讲解</h3>
<p><strong>作用：</strong></p>
<p>LIMIT<code>是</code>限制<code>的意思，所以</code>LIMIT`的作用就是限制查询记录的条数。</p>
<p><strong>LIMIT语句格式</strong>:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名 limit  <span class="keyword">offset</span>, row_count;</span><br><span class="line">mysql中limit的用法：返回前几条或者中间某几行数据</span><br><span class="line"><span class="comment">-- 1 表示分页查询的索引，对应数据表是第二行数据，4表示每页显示4条数据</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名 limit <span class="number">1</span>,<span class="number">4</span>。</span><br><span class="line"><span class="number">1</span>表示索引，注意这里的索引从<span class="number">0</span>开始。对应表中第一行数据</span><br><span class="line"><span class="number">4</span>表示查询记录数。</span><br><span class="line">上述就表示从第<span class="number">2</span>条记录开始查询，一共查询<span class="number">4</span>条，即到第<span class="number">5</span>条。</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>具体步骤：</p>
<p>准备数据：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> student3 (</span><br><span class="line">  id <span class="type">int</span>,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">  age <span class="type">int</span>,</span><br><span class="line">  sex <span class="type">varchar</span>(<span class="number">5</span>),</span><br><span class="line">  address <span class="type">varchar</span>(<span class="number">100</span>),</span><br><span class="line">  math <span class="type">int</span>,</span><br><span class="line">  english <span class="type">int</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> student3(id,NAME,age,sex,address,math,english) <span class="keyword">VALUES</span> (<span class="number">1</span>,<span class="string">&#x27;马云&#x27;</span>,<span class="number">55</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;杭州&#x27;</span>,<span class="number">66</span>,<span class="number">78</span>),(<span class="number">2</span>,<span class="string">&#x27;马化腾&#x27;</span>,<span class="number">45</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;深圳&#x27;</span>,<span class="number">98</span>,<span class="number">87</span>),(<span class="number">3</span>,<span class="string">&#x27;马景涛&#x27;</span>,<span class="number">55</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;香港&#x27;</span>,<span class="number">56</span>,<span class="number">77</span>),(<span class="number">4</span>,<span class="string">&#x27;柳岩&#x27;</span>,<span class="number">20</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;湖南&#x27;</span>,<span class="number">76</span>,<span class="number">65</span>),(<span class="number">5</span>,<span class="string">&#x27;柳青&#x27;</span>,<span class="number">20</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;湖南&#x27;</span>,<span class="number">86</span>,<span class="keyword">NULL</span>),(<span class="number">6</span>,<span class="string">&#x27;刘德华&#x27;</span>,<span class="number">57</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;香港&#x27;</span>,<span class="number">99</span>,<span class="number">99</span>),(<span class="number">7</span>,<span class="string">&#x27;马德&#x27;</span>,<span class="number">22</span>,<span class="string">&#x27;女&#x27;</span>,<span class="string">&#x27;香港&#x27;</span>,<span class="number">99</span>,<span class="number">99</span>),(<span class="number">8</span>,<span class="string">&#x27;德玛西亚&#x27;</span>,<span class="number">18</span>,<span class="string">&#x27;男&#x27;</span>,<span class="string">&#x27;南京&#x27;</span>,<span class="number">56</span>,<span class="number">65</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>查询学生表中数据，跳过前面1条，显示4条</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">我们可以认为跳过前面<span class="number">1</span>条，取<span class="number">4</span>条数据</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> student3 LIMIT <span class="number">1</span>,<span class="number">4</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180102209.bmp"></p>
<p><strong>LIMIT的使用场景</strong>：分页</p>
<p>比如我们登录京东，淘宝，返回的商品信息可能有几万条，不是一次全部显示出来。是一页显示固定的条数。假设我们一每页显示5条记录的方式来分页。</p>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180103700.png"></p>
<p>假设我们一每页显示5条记录的方式来分页，SQL语句如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 每页显示5条</span></span><br><span class="line"><span class="comment">-- 第一页： LIMIT 0,5;	跳过0条，显示5条</span></span><br><span class="line"><span class="comment">-- 第二页： LIMIT 5,5;  跳过5条，显示5条</span></span><br><span class="line"><span class="comment">-- 第三页： LIMIT 10,5; 跳过10条，显示5条</span></span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> student3 LIMIT <span class="number">0</span>,<span class="number">5</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> student3 LIMIT <span class="number">5</span>,<span class="number">5</span>;</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> student3 LIMIT <span class="number">10</span>,<span class="number">5</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/202405180103403.png"></p>
<blockquote>
<p><strong>注意</strong>：</p>
<ul>
<li>如果第一个参数是0可以简写：
<code>SELECT * FROM student3 LIMIT 0,5;</code>
<code>SELECT * FROM student3 LIMIT 5;</code></li>
<li>LIMIT 10，5; -- 不够5条，有多少显示多少</li>
</ul>
</blockquote>
<h3 id="小结">小结</h3>
<ol type="1">
<li><p>LIMIT语句的使用格式？</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 字段 <span class="keyword">FROM</span> 表名 LIMIT 索引, 显示条数;</span><br><span class="line">索引：从<span class="number">0</span>开始，一直变化</span><br><span class="line">显示条数：每页显示的行数，固定不变的</span><br></pre></td></tr></table></figure></li>
<li><p>```sql SELECT 字段名(5) FROM 表名(1) WHERE 条件(2) GROUP BY
分组列名(3) HAVING 条件(4) ORDER BY 排序列名(6) LIMIT 跳过行数,
返回行数(7); 执行顺序：1234567 <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">## 2、数据库约束的概述</span><br><span class="line"></span><br><span class="line">### 目标</span><br><span class="line"></span><br><span class="line">能够说出数据库约束的作用</span><br><span class="line"></span><br><span class="line">### 讲解</span><br><span class="line"></span><br><span class="line">#### 数据库约束的作用</span><br><span class="line"></span><br><span class="line">​	对表中的数据进行进一步的限制，保证数据的**正确性**、**有效性**和**完整性**。</span><br><span class="line"></span><br><span class="line">#### 约束种类</span><br><span class="line"></span><br><span class="line">- `PRIMARY KEY`: 主键约束</span><br><span class="line">- `UNIQUE`: 唯一约束</span><br><span class="line">- `NOT NULL`: 非空约束</span><br><span class="line">- `DEFAULT`: 默认值 了解</span><br><span class="line">- `FOREIGN KEY`: 外键约束</span><br><span class="line"></span><br><span class="line">### 小结</span><br><span class="line"></span><br><span class="line">1. 数据库约束的作用？</span><br><span class="line">   对表中的数据进行进一步的限制，保证数据的**正确性**、**有效性**和**完整性**。</span><br><span class="line"></span><br><span class="line">## 3、主键约束(掌握)</span><br><span class="line"></span><br><span class="line">### 目标</span><br><span class="line"></span><br><span class="line">1. 能够说出主键约束的作用</span><br><span class="line">2. 能够添加主键</span><br><span class="line"></span><br><span class="line">### 讲解</span><br><span class="line"></span><br><span class="line">#### 主键的作用</span><br><span class="line"></span><br><span class="line">**用来唯一标识一条记录**。</span><br><span class="line"></span><br><span class="line">#### 为什么需要主键约束</span><br><span class="line"></span><br><span class="line">有些记录的 name,age,score 字段的值都一样时,那么就没法区分这些数据,造成数据库的记录不唯一,这样就不方便管理数据。</span><br><span class="line">![](https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE01.png)</span><br><span class="line">![](https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE02.png)</span><br><span class="line"></span><br><span class="line">每张表都应该有一个主键，并且每张表只能有一个主键。</span><br><span class="line"></span><br><span class="line">#### 哪个字段作为表的主键</span><br><span class="line"></span><br><span class="line">​	通常不用业务字段作为主键，单独给每张表设计一个id的字段，把id作为主键。主键是给数据库和程序使用的，不是给最终的客户使用的。所以主键有没有含义没有关系，只要不重复，非空就行。 </span><br><span class="line"></span><br><span class="line">#### 创建主键</span><br><span class="line"></span><br><span class="line">主键：`PRIMARY KEY`</span><br><span class="line">**主键的特点**：</span><br><span class="line"></span><br><span class="line">- 主键必须包含唯一的值</span><br><span class="line">- 主键列不能包含NULL值</span><br><span class="line"></span><br><span class="line">**创建主键方式**：</span><br><span class="line"></span><br><span class="line">1. **在创建表的时候给字段添加主键**</span><br><span class="line"></span><br></pre></td></tr></table></figure> 字段名 字段类型 PRIMARY
KEY <figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">2. **在已有表中添加主键(了解)**</span><br><span class="line"></span><br><span class="line">   ```sql</span><br><span class="line">   ALTER TABLE 表名 ADD PRIMARY KEY(字段名);</span><br></pre></td></tr></table></figure></p></li>
</ol>
<p>具体操作：</p>
<ul>
<li>创建表学生表st5, 包含字段(id, name, age)将id做为主键</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> st5 (</span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY, <span class="comment">-- id是主键</span></span><br><span class="line">	NAME <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">	age <span class="type">INT</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE03.png"></p>
<ul>
<li>添加数据</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;唐伯虎&#x27;</span>,<span class="number">20</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">&#x27;周文宾&#x27;</span>,<span class="number">24</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">&#x27;祝枝山&#x27;</span>,<span class="number">22</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="string">&#x27;文征明&#x27;</span>,<span class="number">26</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>插入重复的主键值</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 主键是唯一的不能重复：Duplicate entry &#x27;1&#x27; for key &#x27;PRIMARY&#x27;</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;文征明2&#x27;</span>,<span class="number">30</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>插入NULL的主键值</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 主键是不能为空的：Column &#x27;id&#x27; cannot be null</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st5 (id, NAME,age) <span class="keyword">VALUES</span> (<span class="keyword">NULL</span>, <span class="string">&#x27;文征明3&#x27;</span>,<span class="number">18</span>);</span><br></pre></td></tr></table></figure>
<h3 id="小结-1">小结</h3>
<ol type="1">
<li><p>说出主键约束的作用？唯一,区分一条记录</p></li>
<li><p>主键的特点？唯一,不能为NULL</p></li>
<li><p>添加主键？ <strong>字段名 字段类型 PRIMARY KEY</strong></p>
<p>ALTER TABLE 表名 ADD PRIMARY KEY(字段名);</p></li>
</ol>
<h2 id="主键自增">4、主键自增</h2>
<h3 id="目标-1">目标</h3>
<p>能够设置主键为自动增长</p>
<h3 id="讲解-1">讲解</h3>
<p>​
主键如果让我们自己添加很有可能重复,我们通常希望在每次插入新记录时,数据库自动生成主键字段的值</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字段名 字段类型 <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT</span><br></pre></td></tr></table></figure>
<p><code>AUTO_INCREMENT</code>
表示自动增长(<strong>字段类型必须是整数类型</strong>)</p>
<p>具体操作：</p>
<ul>
<li>创建学生表st6, 包含字段(id, name, age)将id做为主键并自动增长</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> st6 (</span><br><span class="line">	id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT,</span><br><span class="line">	NAME <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">	age <span class="type">INT</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>插入数据</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 主键默认从1开始自动增长</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st6 (NAME, age) <span class="keyword">VALUES</span> (<span class="string">&#x27;唐僧&#x27;</span>, <span class="number">22</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st6 (NAME, age) <span class="keyword">VALUES</span> (<span class="string">&#x27;孙悟空&#x27;</span>, <span class="number">26</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st6 (NAME, age) <span class="keyword">VALUES</span> (<span class="string">&#x27;猪八戒&#x27;</span>, <span class="number">25</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st6 (NAME, age) <span class="keyword">VALUES</span> (<span class="string">&#x27;沙僧&#x27;</span>, <span class="number">20</span>);</span><br></pre></td></tr></table></figure>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE05.png"></p>
<p>DELETE和TRUNCATE的区别</p>
<ul>
<li>DELETE 删除表中的数据，但不重置AUTO_INCREMENT的值。 <img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE06.png"></li>
<li>TRUNCATE 摧毁表，重建表，AUTO_INCREMENT重置为1 <img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E4%B8%BB%E9%94%AE07.png"></li>
</ul>
<h3 id="小结-2">小结</h3>
<ol type="1">
<li>设置主键为自动增长格式？ 字段名 数据类型 PRIMARY KEY
AUTO_INCREMENT</li>
</ol>
<h2 id="唯一约束">5、唯一约束</h2>
<h3 id="目标-2">目标</h3>
<ol type="1">
<li>能够说出唯一约束的作用</li>
<li>能够添加唯一约束</li>
</ol>
<h3 id="讲解-2">讲解</h3>
<h4 id="唯一约束的作用">唯一约束的作用</h4>
<p>在这张表中这个字段的值不能重复</p>
<h4 id="唯一约束的基本格式">唯一约束的基本格式</h4>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字段名 字段类型 <span class="keyword">UNIQUE</span></span><br></pre></td></tr></table></figure>
<p>具体步骤：</p>
<ul>
<li>创建学生表st7, 包含字段(id,
name),name这一列设置唯一约束,不能出现同名的学生</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> st7 (</span><br><span class="line">	id <span class="type">INT</span>,</span><br><span class="line">	NAME <span class="type">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">UNIQUE</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加一些学生</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st7 <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;貂蝉&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st7 <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">&#x27;西施&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st7 <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">&#x27;王昭君&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st7 <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="string">&#x27;杨玉环&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 插入相同的名字出现name重复: Duplicate entry &#x27;貂蝉&#x27; for key &#x27;name&#x27;</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st7 <span class="keyword">VALUES</span> (<span class="number">5</span>, <span class="string">&#x27;貂蝉&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 出现多个null的时候会怎样？因为null是没有值，所以不存在重复的问题</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st3 <span class="keyword">VALUES</span> (<span class="number">5</span>, <span class="keyword">NULL</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st3 <span class="keyword">VALUES</span> (<span class="number">6</span>, <span class="keyword">NULL</span>);</span><br></pre></td></tr></table></figure>
<h3 id="小结-3">小结</h3>
<ol type="1">
<li>说出唯一约束的作用？让这个字段的值不能重复，多个null不算重复</li>
<li>添加唯一约束格式？字段名 字段类型 UNIQUE</li>
</ol>
<h2 id="非空约束">6、非空约束</h2>
<h3 id="目标-3">目标</h3>
<ol type="1">
<li>能够说出非空约束的作用</li>
<li>能够添加非空约束</li>
</ol>
<h3 id="讲解-3">讲解</h3>
<h4 id="非空约束的作用">非空约束的作用</h4>
<p>这个字段必须设置值,不能是NULL</p>
<h4 id="非空约束的基本语法格式">非空约束的基本语法格式</h4>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字段名 字段类型 <span class="keyword">NOT</span> <span class="keyword">NULL</span></span><br></pre></td></tr></table></figure>
<p>具体操作：</p>
<ul>
<li>创建表学生表st8, 包含字段(id,name,gender)其中name不能为NULL</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> st8 (</span><br><span class="line">	id <span class="type">INT</span>,</span><br><span class="line">	NAME <span class="type">VARCHAR</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">	gender <span class="type">CHAR</span>(<span class="number">2</span>)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加一些完整的记录</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st8 <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;郭富城&#x27;</span>, <span class="string">&#x27;男&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st8 <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">&#x27;黎明&#x27;</span>, <span class="string">&#x27;男&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st8 <span class="keyword">VALUES</span> (<span class="number">3</span>, <span class="string">&#x27;张学友&#x27;</span>, <span class="string">&#x27;男&#x27;</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st8 <span class="keyword">VALUES</span> (<span class="number">4</span>, <span class="string">&#x27;刘德华&#x27;</span>, <span class="string">&#x27;男&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 姓名不赋值出现姓名不能为null: Column &#x27;name&#x27; cannot be null</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st8 <span class="keyword">VALUES</span> (<span class="number">5</span>, <span class="keyword">NULL</span>, <span class="string">&#x27;男&#x27;</span>);</span><br></pre></td></tr></table></figure>
<h3 id="小结-4">小结</h3>
<p>1.非空约束的格式:</p>
<p>​ 字段名 数据类型 NOT NULL</p>
<p>2.问题： 主键约束和唯一约束并且非空的区别： id primary key 主键约束
可以实现主键自增，一般数据表只有id是主键约束 name unique not null
唯一并且非空 不能实现主键自增 数据表中很多字段都可以是唯一 非空</p>
<h2 id="默认值了解">7、默认值(了解)</h2>
<h3 id="目标-4">目标</h3>
<ol type="1">
<li>能够说出默认值的作用</li>
<li>能够给字段添加默认值</li>
</ol>
<h3 id="讲解-4">讲解</h3>
<h4 id="默认值的作用">默认值的作用</h4>
<p>往表中添加数据时,如果不指定这个字段的数据,就使用默认值</p>
<h4 id="默认值格式">默认值格式</h4>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">字段名 字段类型 <span class="keyword">DEFAULT</span> 默认值</span><br></pre></td></tr></table></figure>
<p>具体步骤：</p>
<ul>
<li>创建一个学生表 st9，包含字段(id,name,address)，
地址默认值是广州</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> st9 (</span><br><span class="line">	id <span class="type">INT</span>,</span><br><span class="line">	NAME <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">	address <span class="type">VARCHAR</span>(<span class="number">50</span>) <span class="keyword">DEFAULT</span> <span class="string">&#x27;广州&#x27;</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加一条记录,使用默认地址</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st9 (id, NAME) <span class="keyword">VALUES</span> (<span class="number">1</span>, <span class="string">&#x27;刘德华&#x27;</span>);</span><br></pre></td></tr></table></figure>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E9%BB%98%E8%AE%A4%E5%80%BC.png"></p>
<ul>
<li>添加一条记录,不使用默认地址</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> st9 <span class="keyword">VALUES</span> (<span class="number">2</span>, <span class="string">&#x27;张学友&#x27;</span>, <span class="string">&#x27;香港&#x27;</span>); </span><br></pre></td></tr></table></figure>
<h3 id="小结-5">小结</h3>
<ol type="1">
<li>说出默认值的作用？不添加这个字段,就会使用默认值</li>
<li>给字段添加默认值格式？ 字段名 字段类型 DEFAULT 默认值</li>
</ol>
<h2 id="表关系的概念和外键约束">8、表关系的概念和外键约束</h2>
<p>在真实的开发中，一个项目中的数据，一般都会保存在同一个数据库中，但是不同的数据需要保存在不同的数据表中。这时不能把所有的数据都保存在同一张表中。</p>
<p>那么在设计保存数据的数据表时，我们就要根据具体的数据进行分析，然后把同一类数据保存在同一张表中，不同的数据进行分表处理。</p>
<p>数据之间必然会有一定的联系，我们把不同的数据保存在不同的数据表中之后，同时还要在数据表中维护这些数据之间的关系。这时就会导致表和表之间必然会有一定的联系。这时要求设计表的人员，就需要考虑不同表之间的具体关系。</p>
<p>在数据库中，表总共存在三种关系，这三种关系如下描述：真实的数据表之间的关系：</p>
<p><strong>多对多关系、一对多（多对一）</strong>、一对一(极少)。(一对一关系就是我们之前学习的Map集合的key-value关系)</p>
<h3 id="多对多掌握">多对多(掌握)</h3>
<p>例如：程序员和项目的关系、老师和学生，学生和课程，顾客和商品的关系等</p>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/1.bmp"></p>
<p>分析：</p>
<p>程序员和项目：</p>
<p>一个程序员可以参与多个项目的开发，一个项目可以由多个程序员来开发。这种关系就称为多对多关系。</p>
<p>当我们把数据之间的关系分析清楚之后，一般我们需要通过E(<span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20vbGluaz91cmw9cnByeVpubEF6bEx6X2V6M1RYY1Z5MGRkSFlBRTNGNi1EM2JnWDBBVGpITFBUY0dhSXJKa0dteEJUdDQtSERNNF9WSTN4NWRfQ0lCaUJhT1VyZjU0NVBYVWtkelZ6SFV6VkdpRGJlNXhpOGU=">Entity<i class="fa fa-external-link-alt"></i></span>)-R(<span class="exturl" data-url="aHR0cDovL3d3dy5iYWlkdS5jb20vbGluaz91cmw9YmctZlBqaWlGZWMwa05iTjNadTZrRzV0cHFsaXU1aWpjTGZOU0hFYXdKWXZhZ3RTYXowb2NyMXI2N185N2VjODJoWmwxQS1ZVGVDQ1NwUl9wY3NOalRSVkgyVVhBcXZBQlhndHlUMmg4dnE=">relation<i class="fa fa-external-link-alt"></i></span>)图来展示。
实体 关系 图</p>
<p>一个Java对象，可以对应数据库中的一张表，而Java中类的属性，可以对应表中的字段。</p>
<p>而在E-R图中：</p>
<p>一张表，可以称为一个<strong>实体</strong>，使用<strong>矩形</strong>表示，每个实体的<strong>属性</strong>（字段，表的列），使用<strong>椭圆</strong>表示。</p>
<p>表和表之间的<strong>关系</strong>，使用<strong>菱形</strong>表示。</p>
<p>实体(程序员)：编号、姓名、薪资。</p>
<p>实体(项目)：编号、名称。</p>
<p>程序员和项目存在关系：一个程序员可以开发多个项目，一个项目可以被多个程序员开发。</p>
<p><strong>说明：如果两张表是多对多的关系，需要创建第三张表，并在第三张表中增加两列，引入其他两张表的主键作为自己的外键。</strong></p>
<p>关系图总结:</p>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/%E5%A4%9A%E5%AF%B9%E5%A4%9A%E5%85%B3%E7%B3%BB%E5%9B%BE%E8%A7%A3.PNG"></p>
<p>创建表的sql语句:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建程序员表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>),</span><br><span class="line">	salary <span class="keyword">double</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 创建项目表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> project(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 创建中间关系表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder_project(</span><br><span class="line">	coder_id <span class="type">int</span>,</span><br><span class="line">	project_id <span class="type">int</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加测试数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">1</span>,<span class="string">&#x27;张三&#x27;</span>,<span class="number">12000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">&#x27;李四&#x27;</span>,<span class="number">15000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">3</span>,<span class="string">&#x27;王五&#x27;</span>,<span class="number">18000</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="number">1</span>,<span class="string">&#x27;QQ项目&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">&#x27;微信项目&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<p>小结：</p>
<p>1.多对多关系，需要创建第三张表来维护两张主表的关系。在中间表中起码要有两列来自于主表的主键。</p>
<h3 id="外键约束掌握">外键约束(掌握)</h3>
<p>创建第三张关系表即中间表，来维护程序员表和项目表之间的关系。</p>
<p>使用中间表的目的是维护两表多对多的关系：</p>
<p>1 中间表插入的数据 必须在多对多的主表中存在。</p>
<p>2
如果主表的记录在中间表维护了关系，就不能随意删除。如果可以删除，中间表就找不到对应的数据了，这样就没有意义了。</p>
<p>上述是中间表存在的意义，可是我们这里所创建的中间表并没有起到上述的作用，而是存在缺点的：</p>
<p>缺点1: 我们是可以向中间表插入不存在的项目编号和程序员编号的。</p>
<p><img data-src="https://gitee.com/ethanliu6/ImgsForMe/raw/master/blog_images/2.bmp"></p>
<p>说明：在程序员和项目表中是不存在编号是30和20的，但是这里依然可以插入不存在的编号。这样做是不可以的，失去了中间表的意义。</p>
<p>缺点2 : 如果中间表存在程序员的编号,
我们是可以删除程序员表对应的记录的。</p>
<p>在中间表中是存在编号为1的程序员的：</p>
<p><img data-src="img/3.bmp"></p>
<p>可是我们却可以删除程序员表coder中的编号为1的程序员：</p>
<p><img data-src="img/4.bmp"></p>
<p>编号为1的程序员张三已经被我们删除，但是在中间表coder_project中仍然还存在编号为1的程序员，这样对于中间表没有意义了。</p>
<p>说明：</p>
<p>创建第三张表的语句：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder_project(</span><br><span class="line"></span><br><span class="line">   coder_id <span class="type">int</span> ,<span class="comment">--这个外键来自于coder表中的主键</span></span><br><span class="line"></span><br><span class="line">   project_id <span class="type">int</span><span class="comment">--这个外键来自于project表中的主键</span></span><br><span class="line"></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>我们在创建第三张关系表时，表中的每一列，都是在使用其他两张表中的列，这时我们需要对第三张表中的列进行相应的约束。</p>
<p><strong>当前第三张表中的列由于都是引用其他表中的列，我们把第三张表中的这些列称为引用其他表的外键约束。</strong></p>
<p>给某个表中的某一列添加外键约束：</p>
<p>简化语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">foreign</span> key( 当前表中的列名 )  <span class="keyword">references</span> 被引用表名(被引用表的列名);</span><br><span class="line"><span class="keyword">foreign</span> key( coder_id )  <span class="keyword">references</span> coder(id);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<p>注意：一般在开发中，被引用表的列名都是被引用表中的主键。</p>
<p>举例：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">constraint</span> [外键约束名称] <span class="keyword">foreign</span> key(当前表中的列名) <span class="keyword">references</span>  被引用表名(被引用表的列名)</span><br><span class="line">举例：<span class="keyword">constraint</span> coder_project_id <span class="keyword">foreign</span> key(coder_id) <span class="keyword">references</span> coder(id);</span><br></pre></td></tr></table></figure>
<p><strong>关键字解释</strong>： constraint: 添加约束，可以不写 foreign
key(当前表中的列名): 将某个字段作为外键 references
被引用表名(被引用表的列名) : 外键引用主表的主键</p>
<p>给第三张表添加外键约束有两种方式：</p>
<p>第一种方式：给已经存在的表添加外键约束：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 来自于程序员表</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> coder_project <span class="keyword">add</span> <span class="keyword">constraint</span> c_id_fk <span class="keyword">foreign</span> key(coder_id) <span class="keyword">references</span> coder(id);</span><br><span class="line"><span class="comment">-- 来自于项目表</span></span><br><span class="line"><span class="keyword">alter</span> <span class="keyword">table</span> coder_project <span class="keyword">add</span> <span class="keyword">constraint</span> p_id_fk <span class="keyword">foreign</span> key(project_id) <span class="keyword">references</span> project(id);</span><br></pre></td></tr></table></figure>
<p>第二种方式：创建表时就添加外键约束：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder_project(</span><br><span class="line">	coder_id <span class="type">int</span>,</span><br><span class="line">	project_id <span class="type">int</span>,</span><br><span class="line">    <span class="keyword">constraint</span> c_id_fk <span class="keyword">foreign</span> key(coder_id) <span class="keyword">references</span> coder(id),</span><br><span class="line">    <span class="keyword">constraint</span> p_id_fk <span class="keyword">foreign</span> key(project_id) <span class="keyword">references</span> project(id)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>了解完如何给第三张表添加外键约束以后，我们就开始给上述创建好的第三张表添加外键约束。</p>
<p>在添加外键约束之前，由于刚才已经修改了表中的数据，所以我们先清空三张表，然后在添加外键约束。</p>
<p>操作步骤：</p>
<p>1、清空上述三张表：</p>
<p><img data-src="img/5.bmp"></p>
<p>2、增加外键约束：</p>
<p><img data-src="img/6.bmp"></p>
<p>3、添加完外键约束以后，就会在可视化工具中的架构设计器上查看表之间的关系</p>
<p>先选中表</p>
<p><img data-src="img/7.bmp"></p>
<p>然后点击右下角：</p>
<p><img data-src="img/8.bmp"></p>
<p>最后的表关系如下：</p>
<p><img data-src="img/9.bmp"></p>
<p>3、向三张表中分别插入数据：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 插入数据</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;张三&#x27;</span>,<span class="number">12000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;李四&#x27;</span>,<span class="number">15000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;王五&#x27;</span>,<span class="number">18000</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;QQ项目&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;微信项目&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">1</span>);</span><br></pre></td></tr></table></figure>
<p>4、测试外键约束是否起到作用：</p>
<p>A:执行以下语句：</p>
<p><img data-src="img/10.bmp"></p>
<p>B:执行以下语句：</p>
<p><img data-src="img/11.bmp"></p>
<p>再次执行上述语句的时候，发现报错了，而数据库表中的数据都没有改变，说明外键约束起到了作用。</p>
<p>小结：</p>
<p>1.为何引入外键约束？</p>
<p>让表的数据有效性，正确性。提高查询效率。</p>
<p>2.添加外键约束语法：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">constraint</span> 外键约束名 <span class="keyword">foreign</span> key(当前表的字段名) <span class="keyword">references</span> 主表(主键);</span><br></pre></td></tr></table></figure>
<p>3.有了外键约束，那么要求添加数据需要先添加主表，然后添加从表。</p>
<p>4.有了外键约束，那么要求删除数据需要先删除从表，然后再删除主表。</p>
<h3 id="外键的级联掌握">外键的级联(掌握)</h3>
<p>在修改和删除主表的主键时，同时更新或删除从表的外键值，称为级联操作
<code>ON UPDATE CASCADE</code> -- 级联更新，主键发生更新时，外键也会更新
<code>ON DELETE CASCADE</code> --
级联删除，主键发生删除时，外键也会删除</p>
<p>具体操作：</p>
<ul>
<li>删除三张表coder、coder_project、project表</li>
<li>重新创建三张表，添加级联更新和级联删除</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建程序员表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>),</span><br><span class="line">	salary <span class="keyword">double</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 创建项目表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> project(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>)</span><br><span class="line">);</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> coder_project(</span><br><span class="line">	coder_id <span class="type">int</span>,</span><br><span class="line">	project_id <span class="type">int</span>,</span><br><span class="line">    <span class="comment">-- 添加外键约束,并且添加级联更新和级联删除</span></span><br><span class="line">    <span class="keyword">constraint</span> c_id_fk <span class="keyword">foreign</span> key(coder_id) <span class="keyword">references</span> coder(id) <span class="keyword">ON</span> <span class="keyword">UPDATE</span> CASCADE <span class="keyword">ON</span> <span class="keyword">DELETE</span> CASCADE,</span><br><span class="line">    <span class="keyword">constraint</span> p_id_fk <span class="keyword">foreign</span> key(project_id) <span class="keyword">references</span> project(id) <span class="keyword">ON</span> <span class="keyword">UPDATE</span> CASCADE <span class="keyword">ON</span> <span class="keyword">DELETE</span> CASCADE</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>再次添加数据到三张表：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 添加测试数据</span></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">1</span>,<span class="string">&#x27;张三&#x27;</span>,<span class="number">12000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">&#x27;李四&#x27;</span>,<span class="number">15000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder <span class="keyword">values</span>(<span class="number">3</span>,<span class="string">&#x27;王五&#x27;</span>,<span class="number">18000</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="number">1</span>,<span class="string">&#x27;QQ项目&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> project <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">&#x27;微信项目&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> coder_project <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">2</span>);</span><br></pre></td></tr></table></figure>
<p><strong>需求1：修改主表coder表的id为3变为4.</strong></p>
<p><img data-src="img/12.bmp"></p>
<p><strong>需求2：删除主表coder表的id是4的行数据。</strong></p>
<p><img data-src="img/12.bmp"></p>
<p><strong>小结：</strong></p>
<p><strong>级联更新：ON UPDATE CASCADE
主键修改后,外键也会跟着修改</strong></p>
<p><strong>级联删除：ON DELETE CASCADE
主键删除后,外键对应的数据也会删除</strong></p>
<h3 id="一对多掌握">一对多(掌握)</h3>
<p>一对多的关系表：其中也有2个实体，但是其中A实体中的数据可以对应另外B实体中的多个数据，反过来B实体中的多个数据只能对应A实体中的一个数据。</p>
<p>例如：作者和小说关系,班级和学生，部门和员工，客户和订单</p>
<p>分析：</p>
<p>作者和小说：</p>
<p>一个作者可以写多部小说，但每一部小说，只能对应具体的一个作者。</p>
<p>具体的关系如下图所示：</p>
<p><img data-src="img/表之间的一对多关系.PNG"></p>
<p>注意：如果是一对多的关系，那么设计表的时候需要在多的一方增加一列，引入一的一方的主键作为自己的外键。</p>
<h3 id="一对一">一对一</h3>
<p>一对一关系表在实际开发中使用的并不多，其中也是2个实体，其中A实体中的数据只能对应B实体中的一个数据，同时B实体中的数据也只能对应A实体中的一个数据。例如：人和身份证对应关系，老公和老婆的对应关系。</p>
<p>而一对一在建表的时候，可以在任意一方的表中添加另外一方的主键作为外键即可。</p>
<p>建表原理如下图所示：</p>
<p><img data-src="img/一对一表设计图解.PNG"></p>
<p>小结：</p>
<p>1.多对多：创建第三张表维护两张主表的关系，在第三张表中起码要有两列来自于两张主表的主键</p>
<p>2.一对多：在多方增加一列，来自于一方的主键作为外键</p>
<p>3.一对一：在任意一方将另一方的主键作为外键</p>
<h2 id="表设计案例练习">9、表设计案例(练习)</h2>
<h3 id="目标-5">目标</h3>
<p>设计学生成绩管理系统数据表(按照给定需求设计即可)</p>
<p>1、每个教师可以教多门课程</p>
<p>2、每个课程由一个老师负责</p>
<p>3、每门课程可以由多个学生选修</p>
<p>4、每个学生可以选修多门课程</p>
<p>5、学生选修课程要有成绩</p>
<h3 id="讲解-5">讲解</h3>
<p>具体操作：</p>
<p>1）分析：</p>
<p>​
当我们拿到一个需求之后，首先应该分析这个需求中到底有多少名词，或者是当前这个需求中可以抽象出具体几个E-R图中的实体对象。</p>
<p>分析需求中存在的实体： 实体使用矩形表示。</p>
<p>学生、课程、老师。</p>
<p>当分析清楚具体的实体之后，接着要分析实体具备哪些属性？属性使用椭圆形表示。</p>
<p>学生：学号、姓名等。</p>
<p>课程：课程编号、课程名称等。</p>
<p>老师：工号、姓名等。</p>
<p>最后就要考虑实体和实体之间的关系问题：</p>
<p>老师和课程之间：一对多关系。一个老师可以教授多门课程，一个课程只能由一个老师负责。</p>
<p>学生和课程之间：多对多关系。每个学生可以选修多门课程，每门课程可以被多个学生来选修。</p>
<p>2）关于设计学生成绩管理系统的数据表的E-R图如下所示：</p>
<p><img data-src="img/多表设计练习.PNG"></p>
<p>3）创建表sql语句：</p>
<p>画完E-R图之后，接下来我们就根据E-R图来创建具体的数据库表了。</p>
<p>学生选课管理系统的表创建：</p>
<p>思考：先创建哪张表？</p>
<p>不能先创建课程表，因为课程表需要有教师的工号，也不能创建中间表，因为中间表需要课程表和学生表的id，所以我们可以创建表的顺序如下：</p>
<p>-- 1、教师表</p>
<p>-- 2、课程表</p>
<p>-- 3、学生表</p>
<p>-- 4、学生课程表(中间关系表要有成绩属性)</p>
<p>创建表的语句如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 1、教师表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> teacher(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 2、课程表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>),</span><br><span class="line">	teacher_id <span class="type">int</span>,</span><br><span class="line">	<span class="keyword">foreign</span> key(teacher_id) <span class="keyword">references</span> teacher(id)</span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 3、学生表</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">50</span>)</span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 4、学生课程表(中间关系表要有成绩属性)</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> studentcourse(</span><br><span class="line">	student_id <span class="type">int</span>,</span><br><span class="line">	course_id <span class="type">int</span>,</span><br><span class="line">	score <span class="keyword">double</span>,</span><br><span class="line">	<span class="keyword">foreign</span> key(student_id) <span class="keyword">references</span> student(id),</span><br><span class="line">	<span class="keyword">foreign</span> key(course_id) <span class="keyword">references</span> course(id)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<p>创建表之后的结构：</p>
<p><img data-src="img/多表设计练习最后结果.PNG"></p>
<h2 id="作业多表练习">作业:多表练习</h2>
<h3 id="一对多关系练习">一对多关系练习</h3>
<p>以下案例是我们JavaWeb课程最后的小项目.我们拿出其中一部分需求,根据需求来设计数据库表之间的关系</p>
<p>一个旅游线路分类中有多条旅游线路 , 一条旅游线路属于某一个分类 .
旅游线路表是多表 ,可以在多表上添加一个外键来执行分类表中的主键. <img data-src="img/1对多练习01.png"></p>
<div style="background-color:#999;">
具体操作:
<div>


<ul>
<li>创建旅游线路分类表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_category (</span><br><span class="line">   cid <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT, <span class="comment">-- 旅游线路分类主键</span></span><br><span class="line">   cname <span class="type">VARCHAR</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> <span class="comment">-- 旅游线路分类名称</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加旅游线路分类数据</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> tab_category (cname) <span class="keyword">VALUES</span> (<span class="string">&#x27;周边游&#x27;</span>), (<span class="string">&#x27;出境游&#x27;</span>), (<span class="string">&#x27;国内游&#x27;</span>), (<span class="string">&#x27;港澳游&#x27;</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>创建旅游线路表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_route (</span><br><span class="line">   rid <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT, <span class="comment">-- 旅游线路主键</span></span><br><span class="line">   rname <span class="type">VARCHAR</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span>, <span class="comment">-- 旅游线路名称</span></span><br><span class="line">   price <span class="keyword">DOUBLE</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>,  <span class="comment">-- 价格</span></span><br><span class="line">   cid <span class="type">INT</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>, <span class="comment">-- 所属分类</span></span><br><span class="line">   <span class="keyword">CONSTRAINT</span> ro_cid_ref_cate_id <span class="keyword">FOREIGN</span> KEY(cid) <span class="keyword">REFERENCES</span> tab_category(cid)</span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加旅游线路测试数据(自行添加)</li>
</ul>
<h3 id="多对多关系练习">多对多关系练习</h3>
<p>一个用户可以收藏多个线路，一个线路可以被多个用户收藏
,所以用户和线路之间是多对多的关系.对于多对多的关系我们需要增加一张中间表来维护他们之间的关系
<img data-src="img/多对多练习01.png"></p>
<div style="background-color:#999;">
具体操作:
<div>


<ul>
<li>创建用户表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_user (</span><br><span class="line">   uid <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT, <span class="comment">-- 用户id</span></span><br><span class="line">   username <span class="type">VARCHAR</span>(<span class="number">100</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">UNIQUE</span> <span class="comment">-- 用户名</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<ul>
<li>添加用户数据</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> tab_user <span class="keyword">VALUES</span></span><br><span class="line">(<span class="keyword">NULL</span>, <span class="string">&#x27;老王&#x27;</span>),</span><br><span class="line">(<span class="keyword">NULL</span>, <span class="string">&#x27;小王&#x27;</span>);</span><br></pre></td></tr></table></figure>
<ul>
<li>创建收藏表</li>
</ul>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> tab_favorite (</span><br><span class="line">   fid <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT, <span class="comment">-- 收藏主键</span></span><br><span class="line">   rid <span class="type">INT</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>, <span class="comment">-- 旅游线路id</span></span><br><span class="line">   <span class="type">DATE</span> <span class="type">DATE</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>, <span class="comment">-- 收藏时间</span></span><br><span class="line">   uid <span class="type">INT</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="comment">-- 用户id</span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>
<blockquote>
<p>注意 : 可以自己尝试着给收藏表添加外键约束</p>
</blockquote>
<ul>
<li>增加收藏表测试数据(自行添加)</li>
</ul>
<h1 id="第二部分多表查询">第二部分(多表查询)</h1>
<h2 id="多表查询介绍掌握">1、多表查询介绍(掌握)</h2>
<h3 id="目标-6">目标</h3>
<p>了解什么是多表查询，及多表查询的两种方式</p>
<h3 id="讲解-6">讲解</h3>
<h4 id="什么是多表查询">什么是多表查询</h4>
<p><strong>同时查询多张表获取到需要的数据</strong>
比如：我们想查询水果的对应价格，需要将水果表和价格表同时进行查询</p>
<p>一种水果一种价格。</p>
<p>一种价格多种水果。</p>
<p>价格和水果属于一对多。</p>
<p>将一方即价格的主键作为多方即水果的外键。</p>
<p><img data-src="img/多表查询1.bmp"></p>
<h4 id="多表查询的分类">多表查询的分类</h4>
<figure>
<img data-src="img/1550291804592.png" alt="1550291804592">
<figcaption aria-hidden="true">1550291804592</figcaption>
</figure>
<h3 id="小结-6">小结</h3>
<p>什么是多表查询？通过查询多张表获取我们想要的数据</p>
<h2 id="笛卡尔积现象">2、笛卡尔积现象</h2>
<h3 id="目标-7">目标</h3>
<p>能够说出什么是笛卡尔积，以及如何消除笛卡尔积</p>
<h3 id="讲解-7">讲解</h3>
<h4 id="准备数据">准备数据</h4>
<p>有两张表，一张是水果表fruit,一张是价格表price。</p>
<p>分析：</p>
<p>一种水果一种价格。</p>
<p>一个价格对应多种水果。</p>
<p>价格和水果是1对多</p>
<p>价格是1 水果是多</p>
<p><strong>表设计原则：将价格的主键作为水果的外键</strong></p>
<p>建表：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 价格 1</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> price( </span><br><span class="line">   id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">   price <span class="keyword">double</span></span><br><span class="line">);</span><br><span class="line"><span class="comment">-- 水果 n</span></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> fruit(</span><br><span class="line">  id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span>,</span><br><span class="line">  price_id <span class="type">int</span>,</span><br><span class="line">  <span class="keyword">foreign</span> key(price_id) <span class="keyword">references</span> price(id)</span><br><span class="line">);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> price <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">2.30</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> price <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">3.50</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> price <span class="keyword">values</span>(<span class="number">4</span>,<span class="keyword">null</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> fruit <span class="keyword">values</span>(<span class="number">1</span>,<span class="string">&#x27;苹果&#x27;</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> fruit <span class="keyword">values</span>(<span class="number">2</span>,<span class="string">&#x27;橘子&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> fruit <span class="keyword">values</span>(<span class="number">3</span>,<span class="string">&#x27;香蕉&#x27;</span>,<span class="keyword">null</span>);</span><br><span class="line"><span class="comment">-- 一种水果有一个价格 一个价格对应多种水果</span></span><br><span class="line"><span class="comment">-- 价格 1 水果 n 水果将价格主键作为外键</span></span><br></pre></td></tr></table></figure>
<h4 id="什么是笛卡尔积现象">什么是笛卡尔积现象</h4>
<p>需求：查询两张表中关于水果的信息，要显示水果名称和水果价格。</p>
<p>具体操作：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">多表查询语法：<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit,price;</span><br></pre></td></tr></table></figure>
<p>查询结果：</p>
<p><img data-src="img/笛卡尔积问题.bmp"></p>
<p>产生上述查询结果的原因：</p>
<p><img data-src="img/笛卡尔积问题图解.bmp"></p>
<p>说明：</p>
<p>fruit表中的每一条记录，都和price表中的每一条进行匹配连接。所得到的最终结果是：fruit表中的条目数乘以price表中的数据的条目数。</p>
<p>将fruit表的每行记录和price表的每行记录组合的结果就是笛卡尔积。</p>
<p>笛卡尔积问题：把多张表放在一起，同时去查询，会得到一个结果，而这结果并不是我们想要的数据，这个结果称为笛卡尔积。</p>
<p>笛卡尔积缺点：查询到的结果冗余了，里面有很多错误的数据，需要过滤。</p>
<p>举例：上述的笛卡尔积结果中只有两行结果是正确的：</p>
<p>1 苹果 1 1 2.3</p>
<p>2 橘子 2 2 3.5</p>
<p>笛卡尔积的数据，对程序是没有意义的，
我们需要对笛卡尔积中的数据再次进行过滤。</p>
<p>对于多表查询操作，需要过滤出满足条件的数据，需要把多个表进行连接，连接之后需要加上过滤的条件。</p>
<h4 id="如何清除笛卡尔积现象的影响">如何清除笛卡尔积现象的影响</h4>
<p>解决上述查询的方案：在查询两张表的同时添加条件进行过滤，比如fruit表的id和必须和price表的id相同。</p>
<p><img data-src="img/显示内连接解决笛卡尔积问题.bmp"></p>
<h3 id="小结-7">小结</h3>
<ol type="1">
<li><p>能够说出什么是笛卡尔积?
左表的每条记录和右表的每条记录会组合起来</p></li>
<li><p>如何消除笛卡尔积
只查询满足要求的数据，通常都是外键等于主键</p></li>
<li><p>给表起别名：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 别名写法：表名 as 别名  这里as可以省略</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit <span class="keyword">as</span> f,price <span class="keyword">as</span> p <span class="keyword">where</span> f.price_id <span class="operator">=</span> p.id;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="内连接">3、内连接</h2>
<h3 id="目标-8">目标</h3>
<p>能够掌握内连接的使用</p>
<h3 id="讲解-8">讲解</h3>
<h4 id="什么是内连接">什么是内连接</h4>
<p>用左边表的记录去匹配右边表的记录，如果符合条件的则显示。内连接查询的结果：两表的公共部分。</p>
<p><img data-src="img/内连接结果.bmp"></p>
<h4 id="隐式内连接">隐式内连接</h4>
<p>隐式内连接：看不到<code>JOIN</code>关键字，条件使用<code>WHERE</code>指定</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">语法：</span><br><span class="line"><span class="keyword">select</span> 列名 , 列名 .... <span class="keyword">from</span> 表名<span class="number">1</span>,表名<span class="number">2</span>,.... <span class="keyword">where</span> 表名<span class="number">1.</span>列名 <span class="operator">=</span> 表名<span class="number">2.</span>列名;</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit,price <span class="keyword">where</span> fruit.price_id <span class="operator">=</span> price.id;</span><br></pre></td></tr></table></figure>
<p>说明：在产生两张表的笛卡尔积的数据后，通过条件筛选出正确的结果。</p>
<h4 id="显示内连接掌握">显示内连接(掌握)</h4>
<p>显示内连接：使用<code>INNER JOIN ... ON</code>语句,
可以省略<code>INNER</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">语法：</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名<span class="number">1</span>  <span class="keyword">inner</span> <span class="keyword">join</span> 表名<span class="number">2</span> <span class="keyword">inner</span> <span class="keyword">join</span> 表名<span class="number">3</span> <span class="keyword">inner</span> <span class="keyword">join</span> 表名<span class="number">4</span> ..... <span class="keyword">on</span> 多个条件;</span><br><span class="line">或者</span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表名<span class="number">1</span>  <span class="keyword">join</span> 表名<span class="number">2</span> <span class="keyword">on</span> 条件 </span><br></pre></td></tr></table></figure>
<p>具体操作：</p>
<ul>
<li>使用显示内连接解决上述笛卡尔积问题</li>
</ul>
<p><img data-src="img/显示内连接解决笛卡尔积问题2.bmp"></p>
<p>说明：显示的内连接，一般称为标准的内连接，有inner
join，查询到的数据为两个表经过on条件过滤后的笛卡尔积。</p>
<ul>
<li><strong>应用</strong></li>
</ul>
<hr>
<p>查询苹果的信息，显示苹果id,名字，和价格。</p>
<p>1.确定查询哪些表</p>
<p>2.确定表连接条件，水果表.price_id = 价格表.id 的数据才是有效的</p>
<p>3.确定查询条件，我们查询的是苹果的信息，水果表.name='苹果'</p>
<p>4.确定查询字段，查询苹果的信息，显示苹果id,名字，和价格</p>
<p>5.我们发现写表名有点长，可以给表取别名，显示的字段名也使用别名</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 1.确定查询哪些表</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit f <span class="keyword">inner</span> <span class="keyword">join</span> price p;</span><br><span class="line"><span class="comment">-- 2.确定表连接条件</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit f <span class="keyword">inner</span> <span class="keyword">join</span> price p <span class="keyword">on</span> f.price_id <span class="operator">=</span> p.id;</span><br><span class="line"><span class="comment">-- 3.确定查询条件，我们查询的是苹果的信息，水果表.name=&#x27;苹果&#x27;</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit f <span class="keyword">inner</span> <span class="keyword">join</span> price p <span class="keyword">on</span> f.price_id <span class="operator">=</span> p.id <span class="keyword">where</span> f.name<span class="operator">=</span><span class="string">&#x27;苹果&#x27;</span>;</span><br><span class="line"><span class="comment">-- 4. 确定查询哪些列</span></span><br><span class="line"><span class="keyword">select</span> f.id,f.name,p.id,p.price <span class="keyword">from</span> fruit f <span class="keyword">inner</span> <span class="keyword">join</span> price p <span class="keyword">on</span> f.price_id <span class="operator">=</span> p.id <span class="keyword">where</span> f.name<span class="operator">=</span><span class="string">&#x27;苹果&#x27;</span>;</span><br></pre></td></tr></table></figure>
<h3 id="小结-8">小结</h3>
<ol type="1">
<li>什么是隐式内连接和显示内连接？ 隐式内连接：看不到JOIN：select 列名 ,
列名 .... from 表名1,表名2 where 表名1.列名 = 表名2.列名;
<strong>显示内连接：看得到JOIN：select * from 表名1 inner join 表名2 on
条件;</strong></li>
<li>内连接查询步骤？ 1.确定查询几张表 2.确定表连接条件
3.根据需要在操作</li>
</ol>
<h2 id="左外连接">4、左外连接</h2>
<h3 id="目标-9">目标</h3>
<p>能够掌握左外连接查询</p>
<h3 id="讲解-9">讲解</h3>
<p>左外连接原理如下所示：</p>
<p><img data-src="img/做外连接原理图.bmp"></p>
<p><strong>左外连接</strong>可以理解为：用左边表去右边表中查询对应记录，不管是否找到，都将显示左边表中全部记录。</p>
<p>举例：上述案例中虽然右表没有香蕉对应的价格，也要把他查询出来。</p>
<p>左外连接：使用<code>LEFT OUTER JOIN ... ON</code>，<code>OUTER</code>可以省略</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表<span class="number">1</span> <span class="keyword">left</span> <span class="keyword">outer</span> <span class="keyword">join</span> 表<span class="number">2</span> <span class="keyword">on</span> 条件;</span><br><span class="line">把<span class="keyword">left</span> 关键字之前的表，是定义为左侧。 <span class="keyword">left</span>关键字之后的表，定义右侧。</span><br><span class="line">查询的内容，以左侧的表为主，如果左侧有数据，右侧没有对应的数据，仍然会把左侧数据进行显示。</span><br></pre></td></tr></table></figure>
<p>具体操作：</p>
<ul>
<li>不管能否查到水果对应的价格，都要把水果显示出来。</li>
</ul>
<p><img data-src="img/左外连接图解.bmp"></p>
<p>分析：香蕉是没有价格的，但是由于香蕉位于左边的表中，所以即使香蕉的价格是null，也会将香蕉的信息显示出来。</p>
<p><img data-src="img/左外连接结果2.bmp"></p>
<h3 id="小结-9">小结</h3>
<ol type="1">
<li><p>掌握左外连接查询格式？</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表<span class="number">1</span> <span class="keyword">left</span> <span class="keyword">outer</span> <span class="keyword">join</span> 表<span class="number">2</span> <span class="keyword">on</span> 条件;</span><br><span class="line"><span class="comment">-- 表1看作为左表，表2看做为右表</span></span><br></pre></td></tr></table></figure></li>
<li><p>左外连接查询特点？
在满足要求的基础上保证左表的数据全部显示</p></li>
</ol>
<h2 id="右外连接">5、右外连接</h2>
<h3 id="目标-10">目标</h3>
<p>能够掌握右外连接查询</p>
<h3 id="讲解-10">讲解</h3>
<p>右外连接原理如下所示：</p>
<p><img data-src="img/右外连接原理.bmp"></p>
<p>用右边表去左边表查询对应记录，不管是否找到，右边表全部记录都将显示。</p>
<p>举例：上述案例中不管在左方表能否找到右方价格对应的水果，都要把右方的价格显示出来。</p>
<p>右外连接：使用<code>RIGHT OUTER JOIN ... ON</code>，<code>OUTER</code>可以省略</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">语法：<span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> 表<span class="number">1</span> <span class="keyword">right</span> <span class="keyword">outer</span> <span class="keyword">join</span> 表<span class="number">2</span> <span class="keyword">on</span> 条件;</span><br><span class="line">说明：如果右侧有数据，左侧没匹配到，把右侧的数据显示出来。</span><br><span class="line"><span class="keyword">right</span>之前的是左侧，<span class="keyword">right</span>之后的是右侧。</span><br></pre></td></tr></table></figure>
<p>具体操作：</p>
<ul>
<li>需求：不管能否查到价格对应的水果，都要把价格显示出来。</li>
</ul>
<p><img data-src="img/右外连接实现.bmp"></p>
<p>分析：在price表中id为4到fruit表中查询是没有对应水果描述的，但是使用右外连接也会将price表中的价格查询出来。</p>
<p><img data-src="img/右外连接实现2.bmp"></p>
<p><strong>注意：</strong>其实关于左外连接和右外连接只记住一种就可以，只需将表的前后位置换一下就可以达到互换。</p>
<p>需求：使用左外连接达到上述右外连接的效果。</p>
<p><img data-src="img/右外连接和左外连接互换.bmp"></p>
<h3 id="小结-10">小结</h3>
<ol type="1">
<li><p>掌握右外连接查询格式？</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select * from 表1 right outer join 表2 on 条件;</span><br></pre></td></tr></table></figure></li>
<li><p>右外连接查询特点？
在满足要求的基础上,保证右表的数据全部显示.</p></li>
<li><p>其实对于左右外连接我们只需要掌握一种，即可，因为左右外可以互换。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 右外连接：查询right右边表以及两张表的公共部分</span></span><br><span class="line"><span class="comment">-- 需求：不管能否查到价格对应的水果，都要把价格显示出来。</span></span><br><span class="line"><span class="comment">-- 在right右边的表2是右表，以右表为主</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> fruit f <span class="keyword">right</span> <span class="keyword">outer</span> <span class="keyword">join</span> price p</span><br><span class="line"><span class="keyword">on</span> f.price_id <span class="operator">=</span> p.id;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 需求：使用左外连接使用上述功能：</span></span><br><span class="line"><span class="comment">-- 不管能否查到价格对应的水果，都要把价格显示出来。</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> price p <span class="keyword">left</span> <span class="keyword">outer</span> <span class="keyword">join</span>  fruit f </span><br><span class="line"><span class="keyword">on</span> f.price_id <span class="operator">=</span> p.id;</span><br></pre></td></tr></table></figure></li>
</ol>
<h2 id="子查询">6、子查询</h2>
<h3 id="目标-11">目标</h3>
<p>能够掌握子查询的概念</p>
<p>能够理解子查询的三种情况</p>
<h3 id="讲解-11">讲解</h3>
<p>准备数据：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 创建部门表 1</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> dept (</span><br><span class="line">  id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT,</span><br><span class="line">  NAME <span class="type">VARCHAR</span>(<span class="number">20</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept (NAME) <span class="keyword">VALUES</span> (<span class="string">&#x27;开发部&#x27;</span>),(<span class="string">&#x27;市场部&#x27;</span>),(<span class="string">&#x27;财务部&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 创建员工表 n</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> emp (</span><br><span class="line">  id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY AUTO_INCREMENT,</span><br><span class="line">  NAME <span class="type">VARCHAR</span>(<span class="number">10</span>),</span><br><span class="line">  gender <span class="type">CHAR</span>(<span class="number">1</span>),   <span class="comment">-- 性别</span></span><br><span class="line">  salary <span class="keyword">DOUBLE</span>,   <span class="comment">-- 工资</span></span><br><span class="line">  join_date <span class="type">DATE</span>,  <span class="comment">-- 入职日期</span></span><br><span class="line">  dept_id <span class="type">INT</span>,</span><br><span class="line">  <span class="keyword">foreign</span> key(dept_id) <span class="keyword">references</span> dept(id)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(NAME,gender,salary,join_date,dept_id) <span class="keyword">VALUES</span>(<span class="string">&#x27;孙悟空&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">7200</span>,<span class="string">&#x27;2013-02-24&#x27;</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(NAME,gender,salary,join_date,dept_id) <span class="keyword">VALUES</span>(<span class="string">&#x27;猪八戒&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">3600</span>,<span class="string">&#x27;2010-12-02&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(NAME,gender,salary,join_date,dept_id) <span class="keyword">VALUES</span>(<span class="string">&#x27;唐僧&#x27;</span>,<span class="string">&#x27;男&#x27;</span>,<span class="number">9000</span>,<span class="string">&#x27;2008-08-08&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(NAME,gender,salary,join_date,dept_id) <span class="keyword">VALUES</span>(<span class="string">&#x27;白骨精&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="number">5000</span>,<span class="string">&#x27;2015-10-07&#x27;</span>,<span class="number">3</span>);</span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(NAME,gender,salary,join_date,dept_id) <span class="keyword">VALUES</span>(<span class="string">&#x27;蜘蛛精&#x27;</span>,<span class="string">&#x27;女&#x27;</span>,<span class="number">4500</span>,<span class="string">&#x27;2011-03-14&#x27;</span>,<span class="number">1</span>);</span><br></pre></td></tr></table></figure>
<figure>
<img data-src="img/image-20200802103333568.png" alt="image-20200802103333568">
<figcaption aria-hidden="true">image-20200802103333568</figcaption>
</figure>
<h4 id="什么是子查询">什么是子查询</h4>
<h5 id="一条查询语句结果作为另一条查询语法一部分"><strong>一条查询语句结果作为另一条查询语法一部分。</strong></h5>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 查询字段 <span class="keyword">FROM</span> 表 <span class="keyword">WHERE</span> 条件;</span><br><span class="line">举例：</span><br><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> employee <span class="keyword">WHERE</span> salary<span class="operator">=</span>(<span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> employee);</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询01-16418728627231.png">
说明：子查询需要放在（）中</p>
<h4 id="子查询结果的三种情况">子查询结果的三种情况</h4>
<ol type="1">
<li>子查询的结果是单行单列的时候 <img data-src="img/1562225585954.png"></li>
<li>子查询的结果是多行单列的时候 <img data-src="img/1562225605676.png"></li>
<li>子查询的结果是多行多列 <img data-src="img/1562225632007.png"></li>
</ol>
<h3 id="小结-11">小结</h3>
<ol type="1">
<li>什么是子查询？ 一个查询的结果作为另一个查询语句的一部分</li>
<li>子查询结果的三种情况？ 单行单列 多行单列 多行多列</li>
</ol>
<h2 id="子查询的结果是单行单列的时候">7、子查询的结果是单行单列的时候</h2>
<h3 id="目标-12">目标</h3>
<p>能够掌握子查询的结果是单行单列的查询</p>
<h3 id="讲解-12">讲解</h3>
<p>子查询结果是<code>单列</code>，在<code>WHERE</code>后面作为<code>条件</code>
<code>SELECT 查询字段 FROM 表 WHERE 字段=（子查询）;</code></p>
<ol type="1">
<li><p><strong>查询工资最高的员工是谁？</strong></p>
<ol type="1">
<li>查询最高工资是多少</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> emp;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询05-16418728627242.png"></p>
<p>2.根据最高工资到员工表查询到对应的员工信息</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> salary<span class="operator">=</span>(<span class="keyword">SELECT</span> <span class="built_in">MAX</span>(salary) <span class="keyword">FROM</span> emp);</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询06-16418728627243.png"></p></li>
<li><p><strong>查询工资小于平均工资的员工有哪些？</strong></p>
<ol type="1">
<li>查询平均工资是多少</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) <span class="keyword">FROM</span> emp;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询07-16418728627244.png"></p>
<p>​ 2.到员工表查询小于平均的员工信息</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> salary <span class="operator">&lt;</span> (<span class="keyword">SELECT</span> <span class="built_in">AVG</span>(salary) <span class="keyword">FROM</span> emp);</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询08-16418728627247.png"></p></li>
</ol>
<h3 id="小结-12">小结</h3>
<p>子查询的结果是单行单列时父查询如何处理？ SELECT 查询字段 FROM 表
WHERE 字段=（子查询）;</p>
<p>通常作为父查询的条件</p>
<h2 id="子查询结果是多行单列的时候">8、子查询结果是多行单列的时候</h2>
<h3 id="目标-13">目标</h3>
<p>能够掌握子查询的结果是多行单列的查询</p>
<h3 id="讲解-13">讲解</h3>
<p>子查询结果是多行单列，结果集类似于一个数组，在<code>WHERE</code>后面作为<code>条件</code>，父查询使用<code>IN</code>运算符</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 查询字段 <span class="keyword">FROM</span> 表 <span class="keyword">WHERE</span> 字段 <span class="keyword">IN</span> （子查询）;</span><br><span class="line"><span class="comment">-- in表示在数值中</span></span><br></pre></td></tr></table></figure>
<ol type="1">
<li><p><strong>查询工资大于5000的员工，来自于哪些部门的名字</strong></p>
<ol type="1">
<li>先查询大于5000的员工所在的部门id</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> dept_id <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> salary <span class="operator">&gt;</span> <span class="number">5000</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询09-16418728627245.png"></p>
<ol type="1">
<li>再查询在这些部门id中部门的名字</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> dept.name <span class="keyword">FROM</span> dept <span class="keyword">WHERE</span> dept.id <span class="keyword">IN</span> (<span class="keyword">SELECT</span> dept_id <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> salary <span class="operator">&gt;</span> <span class="number">5000</span>);</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询10-16418728627246.png"></p></li>
<li><p><strong>查询开发部与财务部所有的员工信息</strong></p>
<ol type="1">
<li>先查询开发部与财务部的id</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> id <span class="keyword">FROM</span> dept <span class="keyword">WHERE</span> NAME <span class="keyword">IN</span>(<span class="string">&#x27;开发部&#x27;</span>,<span class="string">&#x27;财务部&#x27;</span>);</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询11-16418728627248.png"></p>
<ol type="1">
<li>再查询在这些部门id中有哪些员工</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> dept_id <span class="keyword">IN</span> (<span class="keyword">SELECT</span> id <span class="keyword">FROM</span> dept <span class="keyword">WHERE</span> NAME <span class="keyword">IN</span>(<span class="string">&#x27;开发部&#x27;</span>,<span class="string">&#x27;财务部&#x27;</span>));</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询12-16418728627249.png"></p></li>
</ol>
<h3 id="小结-13">小结</h3>
<p>1.子查询的结果是多行单列时父查询如何处理？
放在父查询的条件位置,使用in</p>
<p>2.in用法：in(值1,值2,....) 只要满足一个即可，或者的关系</p>
<h2 id="子查询的结果是多行多列">9、子查询的结果是多行多列</h2>
<h3 id="目标-14">目标</h3>
<p>能够掌握子查询的结果是多行多列的查询</p>
<h3 id="讲解-14">讲解</h3>
<p>子查询结果是<code>多列</code>，在<code>FROM</code>后面作为<code>表</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> 查询字段 <span class="keyword">FROM</span> （子查询） 表别名 <span class="keyword">WHERE</span> 条件;</span><br></pre></td></tr></table></figure>
<p><strong>注意：子查询作为表需要取别名，使用as，可以省略。否则这张表没用名称无法访问表中的字段</strong></p>
<ul>
<li><p><strong>查询出2011年以后入职的员工信息，包括部门名称</strong></p>
<ol type="1">
<li>在员工表中查询2011-1-1以后入职的员工</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> join_date <span class="operator">&gt;</span> <span class="string">&#x27;2011-1-1&#x27;</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询14-164187286272410.png"></p>
<ol type="1">
<li>查询所有的部门信息，与上面的虚拟表中的信息组合，找出所有部门id等于dept_id</li>
</ol>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> dept d, (<span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp <span class="keyword">WHERE</span> join_date <span class="operator">&gt;</span> <span class="string">&#x27;2011-1-1&#x27;</span>) e <span class="keyword">WHERE</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/子查询13-164187286272411.png"></p></li>
</ul>
<p>使用表连接：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> d.<span class="operator">*</span>, e.<span class="operator">*</span> <span class="keyword">FROM</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> emp e <span class="keyword">ON</span> d.id <span class="operator">=</span> e.dept_id <span class="keyword">WHERE</span> e.join_date <span class="operator">&gt;</span> <span class="string">&#x27;2011-1-1&#x27;</span>;</span><br></pre></td></tr></table></figure>
<h3 id="小结-14">小结</h3>
<p>三种子查询情况：单行单列，多行单列，多行多列
单行单列：作为父查询的条件 多行单列：作为父查询的条件,通常使用 IN
多行多列：作为父查询的一张表(虚拟表)，起别名</p>
<h2 id="多表查询案例子查询">10、多表查询案例(子查询)</h2>
<p>​
我们在公司开发中，根据不同的业务需求往往需要通过2张及以上的表中去查询需要的数据。所以我们有必要学习2张及以上的表的查询。其实不管是几张表的查询，都是有规律可循的。</p>
<h3 id="准备数据-1">准备数据</h3>
<p>重新新建一个数据库，然后在新建的数据库中创建如下数据表，并插入数据：</p>
<p>teacher 教师表</p>
<p>student 学生表</p>
<p>cource 课程表</p>
<p>studentcource 选课表 学生和课程的关系表</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> teacher (</span><br><span class="line">  id <span class="type">int</span>(<span class="number">11</span>) <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">unique</span></span><br><span class="line"> );</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> student (</span><br><span class="line">  id <span class="type">int</span>(<span class="number">11</span>)  <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span> <span class="keyword">unique</span>,</span><br><span class="line">  city <span class="type">varchar</span>(<span class="number">40</span>) <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">  age <span class="type">int</span> </span><br><span class="line">) ;</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> course(</span><br><span class="line">  id <span class="type">int</span>(<span class="number">11</span>)  <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">  name <span class="type">varchar</span>(<span class="number">20</span>) <span class="keyword">not</span> <span class="keyword">null</span> <span class="keyword">unique</span>,</span><br><span class="line">  teacher_id <span class="type">int</span>(<span class="number">11</span>) <span class="keyword">not</span> <span class="keyword">null</span>,</span><br><span class="line">  <span class="keyword">foreign</span> key(teacher_id) <span class="keyword">references</span> teacher (id)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> studentcourse (</span><br><span class="line">   student_id <span class="type">int</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">   course_id <span class="type">int</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">   score <span class="keyword">double</span> <span class="keyword">NOT</span> <span class="keyword">NULL</span>,</span><br><span class="line">   <span class="keyword">foreign</span> key (student_id) <span class="keyword">references</span> student (id),</span><br><span class="line">   <span class="keyword">foreign</span> key (course_id) <span class="keyword">references</span> course (id)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> teacher <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;关羽&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> teacher <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;张飞&#x27;</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> teacher <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;赵云&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小王&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>,<span class="number">20</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小李&#x27;</span>,<span class="string">&#x27;上海&#x27;</span>,<span class="number">18</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小周&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>,<span class="number">22</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小刘&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>,<span class="number">21</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小张&#x27;</span>,<span class="string">&#x27;上海&#x27;</span>,<span class="number">22</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小赵&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>,<span class="number">17</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小蒋&#x27;</span>,<span class="string">&#x27;上海&#x27;</span>,<span class="number">23</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小韩&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>,<span class="number">25</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小魏&#x27;</span>,<span class="string">&#x27;上海&#x27;</span>,<span class="number">18</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> student <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;小明&#x27;</span>,<span class="string">&#x27;广州&#x27;</span>,<span class="number">20</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;语文&#x27;</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;数学&#x27;</span>,<span class="number">1</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;生物&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;化学&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;物理&#x27;</span>,<span class="number">2</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> course <span class="keyword">values</span>(<span class="keyword">null</span>,<span class="string">&#x27;英语&#x27;</span>,<span class="number">3</span>);</span><br><span class="line"></span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">1</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">2</span>,<span class="number">90</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">3</span>,<span class="number">85</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">1</span>,<span class="number">4</span>,<span class="number">78</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">2</span>,<span class="number">53</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">3</span>,<span class="number">77</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">2</span>,<span class="number">5</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">1</span>,<span class="number">71</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">2</span>,<span class="number">70</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">4</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">5</span>,<span class="number">65</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">3</span>,<span class="number">6</span>,<span class="number">75</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">4</span>,<span class="number">2</span>,<span class="number">90</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">4</span>,<span class="number">3</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">4</span>,<span class="number">4</span>,<span class="number">70</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">4</span>,<span class="number">6</span>,<span class="number">95</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">5</span>,<span class="number">1</span>,<span class="number">60</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">5</span>,<span class="number">2</span>,<span class="number">70</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">5</span>,<span class="number">5</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">5</span>,<span class="number">6</span>,<span class="number">69</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">6</span>,<span class="number">1</span>,<span class="number">76</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">6</span>,<span class="number">2</span>,<span class="number">88</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">6</span>,<span class="number">3</span>,<span class="number">87</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">7</span>,<span class="number">4</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">8</span>,<span class="number">2</span>,<span class="number">71</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">8</span>,<span class="number">3</span>,<span class="number">58</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">8</span>,<span class="number">5</span>,<span class="number">68</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">9</span>,<span class="number">2</span>,<span class="number">88</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">10</span>,<span class="number">1</span>,<span class="number">77</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">10</span>,<span class="number">2</span>,<span class="number">76</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">10</span>,<span class="number">3</span>,<span class="number">80</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">10</span>,<span class="number">4</span>,<span class="number">85</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> studentcourse <span class="keyword">values</span>(<span class="number">10</span>,<span class="number">5</span>,<span class="number">83</span>);</span><br></pre></td></tr></table></figure>
<p>分析4张表的关系：</p>
<p><img data-src="img/多表练习图解1.bmp"></p>
<h3 id="练习1">练习1</h3>
<h4 id="目标-15">目标</h4>
<p>查询获得最高分的学生信息。</p>
<h4 id="讲解-15">讲解</h4>
<p>具体操作：</p>
<p>分析：</p>
<p>1）在中间表中找最高分；</p>
<p>2）在中间表中找最高分对应的学生编号；</p>
<p>3）在学生表中根据学生编号找学生信息；</p>
<p><img data-src="img/子查询练习.bmp"></p>
<h3 id="练习2">练习2</h3>
<h4 id="目标-16">目标</h4>
<p>查询编号是2的课程比编号是1的课程最高成绩高的学生信息。</p>
<h4 id="讲解-16">讲解</h4>
<p>具体操作：</p>
<p>课程编号和对应的成绩的部分数据：</p>
<p><img data-src="img/子查询练习2.bmp"></p>
<p>分析：</p>
<p>1）在中间表中 找编号是1的课程的最高成绩;</p>
<p>2）在中间表中 找编号是2的成绩 &gt; 编号1最高成绩 的学生id;</p>
<p>3）在学生表中 根据学生的编号 找对应的学生信息;</p>
<p>Sql语句如下所示：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 需求2：查询编号2课程 比 编号1课程最高成绩高学生信息：</span></span><br><span class="line"><span class="comment">-- 2.1 在中间表 找编号1课程的最高成绩</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line"><span class="keyword">from</span> studentcourse </span><br><span class="line"><span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>;</span><br><span class="line"><span class="comment">-- 2.2 在中间表找 编号2的成绩 &gt; 编号1最高成绩 的学生id</span></span><br><span class="line"><span class="keyword">select</span> student_id</span><br><span class="line"><span class="keyword">from</span> studentcourse</span><br><span class="line"><span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">2</span> <span class="keyword">and</span> score<span class="operator">&gt;</span>(<span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line">							 <span class="keyword">from</span> studentcourse </span><br><span class="line">							 <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>);</span><br><span class="line"><span class="comment">-- 2.3 在学生表 根据编号 找对应的学生信息</span></span><br><span class="line"><span class="keyword">select</span> <span class="operator">*</span></span><br><span class="line"><span class="keyword">from</span> student</span><br><span class="line"><span class="keyword">where</span> id <span class="keyword">in</span> (<span class="keyword">select</span> student_id</span><br><span class="line">			 <span class="keyword">from</span> studentcourse</span><br><span class="line">			 <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">2</span> <span class="keyword">and</span> score<span class="operator">&gt;</span>(<span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line">										  <span class="keyword">from</span> studentcourse </span><br><span class="line">										  <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>));</span><br></pre></td></tr></table></figure>
<p>查询结果：</p>
<p><img data-src="img/子查询练习3.bmp"></p>
<h3 id="练习3">练习3</h3>
<h4 id="目标-17">目标</h4>
<p>查询编号是2的课程比编号是1的课程最高成绩高的学生姓名和成绩。</p>
<h4 id="讲解-17">讲解</h4>
<p>具体操作：</p>
<p>分析：</p>
<p>1）在中间表中 找编号是1的课程的最高成绩;</p>
<p>2）在中间表中 找编号是2的成绩 &gt; 编号1最高成绩 的学生id和成绩;</p>
<p>3）将上述查询出来的内容作为临时表 和 学生表关联, 查询姓名和成绩</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 需求3：查询编号2课程比编号1课程最高成绩高学生姓名和成绩 （临时表）		</span></span><br><span class="line"><span class="comment">-- 2.1 在中间表 找编号1课程的最高成绩</span></span><br><span class="line"><span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line"><span class="keyword">from</span> studentcourse </span><br><span class="line"><span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>;</span><br><span class="line"><span class="comment">-- 2.2 在中间表 编号2的成绩 &gt; 编号1最高成绩 的学生id,成绩</span></span><br><span class="line"><span class="keyword">select</span> student_id,score</span><br><span class="line"><span class="keyword">from</span> studentcourse</span><br><span class="line"><span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">2</span> <span class="keyword">and</span> score<span class="operator">&gt;</span>(<span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line">							 <span class="keyword">from</span> studentcourse </span><br><span class="line">							 <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>);</span><br><span class="line"><span class="comment">-- 2.3 将上述查询出来的内容作为临时表 和 学生表关联, 查询姓名和成绩</span></span><br><span class="line"><span class="keyword">select</span> student.name, temp.score</span><br><span class="line"><span class="keyword">from</span> student, (<span class="keyword">select</span> student_id,score</span><br><span class="line">			   <span class="keyword">from</span> studentcourse</span><br><span class="line">			   <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">2</span> <span class="keyword">and</span> score<span class="operator">&gt;</span>(<span class="keyword">select</span> <span class="built_in">max</span>(score)</span><br><span class="line">										    <span class="keyword">from</span> studentcourse </span><br><span class="line">										    <span class="keyword">where</span> course_id<span class="operator">=</span><span class="number">1</span>)) <span class="keyword">as</span> temp</span><br><span class="line"><span class="keyword">where</span> student.id<span class="operator">=</span>temp.student_id;</span><br></pre></td></tr></table></figure>
<h3 id="练习4">练习4</h3>
<h4 id="目标-18">目标</h4>
<p><font color="red">查询每个同学的学号、姓名、选课数、总成绩。</font></p>
<h4 id="讲解-18">讲解</h4>
<p>分析：</p>
<p>1）、在中间表中查询每个学生的选课数和总成绩，遇到每个，分组，按照学生学号进行分组;</p>
<p>2）、由于还得显示学号和姓名，并且姓名在student表中，所以我们将上述结果作为临时表和学生表关联。</p>
<p>目的是查找临时表和student表中学号相等时查找学号，姓名，选课数，总成绩。</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 2、查询所有同学的学号、姓名、选课数、总成绩</span></span><br><span class="line"><span class="comment">-- 2.1 在中间表 查询每个学生的选课数和总成绩</span></span><br><span class="line"><span class="keyword">select</span> student_id,<span class="built_in">count</span>(<span class="operator">*</span>),<span class="built_in">sum</span>(score)</span><br><span class="line"><span class="keyword">from</span> studentcourse</span><br><span class="line"><span class="keyword">group</span> <span class="keyword">by</span> student_id;</span><br><span class="line"><span class="comment">-- 2.2 将2.1的结果作为临时表和学生表关联,</span></span><br><span class="line"><span class="comment">-- 目的: 显示 学号、姓名、选课数、总成绩</span></span><br><span class="line"><span class="keyword">select</span> student.id,student.name,temp.cou,temp.sumScore</span><br><span class="line"><span class="keyword">from</span> student,(<span class="keyword">select</span> student_id,<span class="built_in">count</span>(<span class="operator">*</span>) <span class="keyword">as</span> cou,<span class="built_in">sum</span>(score) <span class="keyword">as</span> sumScore</span><br><span class="line">			  <span class="keyword">from</span> studentcourse</span><br><span class="line">			  <span class="keyword">group</span> <span class="keyword">by</span> student_id) <span class="keyword">as</span> temp</span><br><span class="line"><span class="keyword">where</span> student.id<span class="operator">=</span>temp.student_id;</span><br></pre></td></tr></table></figure>
<p>注意：</p>
<p><strong>如果我们想使用聚合函数作为查找的结果，并且聚合函数存在子查询语句中，那么我们不能直接将聚合函数写在select后面，我们此时应该给聚合函数取别名。</strong></p>
<h2 id="多表查询案例连接查询">11、 多表查询案例(连接查询)</h2>
<p>​
我们在公司开发中，根据不同的业务需求往往需要通过2张及以上的表中去查询需要的数据。所以我们有必要学习2张及以上的表的查询。其实不管是几张表的查询，都是有规律可循的。</p>
<h3 id="准备数据-2">1.准备数据</h3>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 部门表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> dept (</span><br><span class="line">  id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY <span class="keyword">PRIMARY</span> KEY, <span class="comment">-- 部门id</span></span><br><span class="line">  dname <span class="type">VARCHAR</span>(<span class="number">50</span>), <span class="comment">-- 部门名称</span></span><br><span class="line">  loc <span class="type">VARCHAR</span>(<span class="number">50</span>) <span class="comment">-- 部门位置</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加4个部门</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> dept(id,dname,loc) <span class="keyword">VALUES</span> </span><br><span class="line">(<span class="number">10</span>,<span class="string">&#x27;教研部&#x27;</span>,<span class="string">&#x27;北京&#x27;</span>),</span><br><span class="line">(<span class="number">20</span>,<span class="string">&#x27;学工部&#x27;</span>,<span class="string">&#x27;上海&#x27;</span>),</span><br><span class="line">(<span class="number">30</span>,<span class="string">&#x27;销售部&#x27;</span>,<span class="string">&#x27;广州&#x27;</span>),</span><br><span class="line">(<span class="number">40</span>,<span class="string">&#x27;财务部&#x27;</span>,<span class="string">&#x27;深圳&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 职务表，职务名称，职务描述</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> job (</span><br><span class="line">  id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY,</span><br><span class="line">  jname <span class="type">VARCHAR</span>(<span class="number">20</span>),</span><br><span class="line">  description <span class="type">VARCHAR</span>(<span class="number">50</span>)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加4个职务</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> job (id, jname, description) <span class="keyword">VALUES</span></span><br><span class="line">(<span class="number">1</span>, <span class="string">&#x27;董事长&#x27;</span>, <span class="string">&#x27;管理整个公司，接单&#x27;</span>),</span><br><span class="line">(<span class="number">2</span>, <span class="string">&#x27;经理&#x27;</span>, <span class="string">&#x27;管理部门员工&#x27;</span>),</span><br><span class="line">(<span class="number">3</span>, <span class="string">&#x27;销售员&#x27;</span>, <span class="string">&#x27;向客人推销产品&#x27;</span>),</span><br><span class="line">(<span class="number">4</span>, <span class="string">&#x27;文员&#x27;</span>, <span class="string">&#x27;使用办公软件&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 员工表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> emp (</span><br><span class="line">  id <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY, <span class="comment">-- 员工id</span></span><br><span class="line">  ename <span class="type">VARCHAR</span>(<span class="number">50</span>), <span class="comment">-- 员工姓名</span></span><br><span class="line">  job_id <span class="type">INT</span>, <span class="comment">-- 职务id</span></span><br><span class="line">  mgr <span class="type">INT</span> , <span class="comment">-- 上级领导</span></span><br><span class="line">  joindate <span class="type">DATE</span>, <span class="comment">-- 入职日期</span></span><br><span class="line">  salary <span class="type">DECIMAL</span>(<span class="number">7</span>,<span class="number">2</span>), <span class="comment">-- 工资</span></span><br><span class="line">  bonus <span class="type">DECIMAL</span>(<span class="number">7</span>,<span class="number">2</span>), <span class="comment">-- 奖金</span></span><br><span class="line">  dept_id <span class="type">INT</span>, <span class="comment">-- 所在部门编号</span></span><br><span class="line">  <span class="keyword">CONSTRAINT</span> emp_jobid_ref_job_id_fk <span class="keyword">FOREIGN</span> KEY (job_id) <span class="keyword">REFERENCES</span> job (id),</span><br><span class="line">  <span class="keyword">CONSTRAINT</span> emp_deptid_ref_dept_id_fk <span class="keyword">FOREIGN</span> KEY (dept_id) <span class="keyword">REFERENCES</span> dept (id)</span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加员工</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> emp(id,ename,job_id,mgr,joindate,salary,bonus,dept_id) <span class="keyword">VALUES</span> </span><br><span class="line">(<span class="number">1001</span>,<span class="string">&#x27;孙悟空&#x27;</span>,<span class="number">4</span>,<span class="number">1004</span>,<span class="string">&#x27;2000-12-17&#x27;</span>,<span class="string">&#x27;8000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">20</span>),</span><br><span class="line">(<span class="number">1002</span>,<span class="string">&#x27;卢俊义&#x27;</span>,<span class="number">3</span>,<span class="number">1006</span>,<span class="string">&#x27;2001-02-20&#x27;</span>,<span class="string">&#x27;16000.00&#x27;</span>,<span class="string">&#x27;3000.00&#x27;</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1003</span>,<span class="string">&#x27;林冲&#x27;</span>,<span class="number">3</span>,<span class="number">1006</span>,<span class="string">&#x27;2001-02-22&#x27;</span>,<span class="string">&#x27;12500.00&#x27;</span>,<span class="string">&#x27;5000.00&#x27;</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1004</span>,<span class="string">&#x27;唐僧&#x27;</span>,<span class="number">2</span>,<span class="number">1009</span>,<span class="string">&#x27;2001-04-02&#x27;</span>,<span class="string">&#x27;29750.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">20</span>),</span><br><span class="line">(<span class="number">1005</span>,<span class="string">&#x27;李逵&#x27;</span>,<span class="number">4</span>,<span class="number">1006</span>,<span class="string">&#x27;2001-09-28&#x27;</span>,<span class="string">&#x27;12500.00&#x27;</span>,<span class="string">&#x27;14000.00&#x27;</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1006</span>,<span class="string">&#x27;宋江&#x27;</span>,<span class="number">2</span>,<span class="number">1009</span>,<span class="string">&#x27;2001-05-01&#x27;</span>,<span class="string">&#x27;28500.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1007</span>,<span class="string">&#x27;刘备&#x27;</span>,<span class="number">2</span>,<span class="number">1009</span>,<span class="string">&#x27;2001-09-01&#x27;</span>,<span class="string">&#x27;24500.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">10</span>),</span><br><span class="line">(<span class="number">1008</span>,<span class="string">&#x27;猪八戒&#x27;</span>,<span class="number">4</span>,<span class="number">1004</span>,<span class="string">&#x27;2007-04-19&#x27;</span>,<span class="string">&#x27;30000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">20</span>),</span><br><span class="line">(<span class="number">1009</span>,<span class="string">&#x27;罗贯中&#x27;</span>,<span class="number">1</span>,<span class="keyword">NULL</span>,<span class="string">&#x27;2001-11-17&#x27;</span>,<span class="string">&#x27;50000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">10</span>),</span><br><span class="line">(<span class="number">1010</span>,<span class="string">&#x27;吴用&#x27;</span>,<span class="number">3</span>,<span class="number">1006</span>,<span class="string">&#x27;2001-09-08&#x27;</span>,<span class="string">&#x27;15000.00&#x27;</span>,<span class="string">&#x27;0.00&#x27;</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1011</span>,<span class="string">&#x27;沙僧&#x27;</span>,<span class="number">4</span>,<span class="number">1004</span>,<span class="string">&#x27;2007-05-23&#x27;</span>,<span class="string">&#x27;11000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">20</span>),</span><br><span class="line">(<span class="number">1012</span>,<span class="string">&#x27;李逵&#x27;</span>,<span class="number">4</span>,<span class="number">1006</span>,<span class="string">&#x27;2001-12-03&#x27;</span>,<span class="string">&#x27;9500.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">30</span>),</span><br><span class="line">(<span class="number">1013</span>,<span class="string">&#x27;小白龙&#x27;</span>,<span class="number">4</span>,<span class="number">1004</span>,<span class="string">&#x27;2001-12-03&#x27;</span>,<span class="string">&#x27;30000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">20</span>),</span><br><span class="line">(<span class="number">1014</span>,<span class="string">&#x27;关羽&#x27;</span>,<span class="number">4</span>,<span class="number">1007</span>,<span class="string">&#x27;2002-01-23&#x27;</span>,<span class="string">&#x27;13000.00&#x27;</span>,<span class="keyword">NULL</span>,<span class="number">10</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 工资等级表</span></span><br><span class="line"><span class="keyword">CREATE</span> <span class="keyword">TABLE</span> salarygrade (</span><br><span class="line">  grade <span class="type">INT</span> <span class="keyword">PRIMARY</span> KEY,</span><br><span class="line">  losalary <span class="type">INT</span>, <span class="comment">-- 最低薪资</span></span><br><span class="line">  hisalary <span class="type">INT</span> <span class="comment">-- 最高薪资</span></span><br><span class="line">);</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 添加5个工资等级</span></span><br><span class="line"><span class="keyword">INSERT</span> <span class="keyword">INTO</span> salarygrade(grade,losalary,hisalary) <span class="keyword">VALUES</span> </span><br><span class="line">(<span class="number">1</span>,<span class="number">7000</span>,<span class="number">12000</span>),</span><br><span class="line">(<span class="number">2</span>,<span class="number">12010</span>,<span class="number">14000</span>),</span><br><span class="line">(<span class="number">3</span>,<span class="number">14010</span>,<span class="number">20000</span>),</span><br><span class="line">(<span class="number">4</span>,<span class="number">20010</span>,<span class="number">30000</span>),</span><br><span class="line">(<span class="number">5</span>,<span class="number">30010</span>,<span class="number">99990</span>);</span><br></pre></td></tr></table></figure>
<p>分析4张表的关系：通过4张表可以查出一个员工的所有信息</p>
<figure>
<img data-src="img/image-20200727092438331.png" alt="image-20200727092438331">
<figcaption aria-hidden="true">image-20200727092438331</figcaption>
</figure>
<p>说明：一个部门有多种职务，例如销售部有经理和销售员。一种职务可以在多个部门，例如文员可以在学工部和教研部。</p>
<p><img data-src="img/多表查询01-1597203787779.png"></p>
<h3 id="练习">2 练习</h3>
<h4 id="练习1-1">1.1 练习1</h4>
<p>查询所有员工信息。显示员工编号，员工姓名，工资，职务名称，职务描述。</p>
<figure>
<img data-src="img/image-20200727093014584.png" alt="image-20200727093014584">
<figcaption aria-hidden="true">image-20200727093014584</figcaption>
</figure>
<p>具体操作： 1.确定要查询哪些表：emp e, job j</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询02-1597203787780.png"></p>
<p>2.确定表连接条件： e.job_id=j.id</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询03-1597203787780.png"> <img data-src="img/多表查询04-1597203787780.png"></p>
<p>3.确定查询字段：员工编号，员工姓名，工资，职务名称，职务描述</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.id, e.ename, e.salary, j.jname, j.description <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询05-1597203787780.png"></p>
<h4 id="练习2-1">1.2 练习2</h4>
<p>查询所有员工信息。显示员工编号，员工姓名，工资，职务名称，职务描述，部门名称，部门位置</p>
<p>具体操作：</p>
<ol type="1">
<li><p>确定要查询哪些表，emp e, job j, dept d</p>
<figure>
<img data-src="img/image-20200727093902316.png" alt="image-20200727093902316">
<figcaption aria-hidden="true">image-20200727093902316</figcaption>
</figure></li>
</ol>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询06-1597203787780.png"> <img data-src="img/多表查询07-1597203787780.png"></p>
<pre><code>  2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询08-1597203787781.png"> <img data-src="img/多表查询09-1597203787781.png"></p>
<pre><code>  3. 确定查询字段：员工编号，员工姓名，工资，职务名称，职务描述，部门名称，部门位置</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">   <span class="keyword">SELECT</span> e.id, e.ename, e.salary, j.jname, j.description, d.dname, d.loc <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id;</span><br><span class="line">   </span><br><span class="line">   或者：</span><br><span class="line">   <span class="keyword">SELECT</span> e.id,e.ename,e.salary,j.jname,j.description,d.dname,d.loc</span><br><span class="line"><span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">ON</span> e.job_id <span class="operator">=</span> j.id <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">on</span> e.dept_id <span class="operator">=</span> d.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询10-1597203787781.png"></p>
<h4 id="练习3-1">1.3 练习3</h4>
<p>查询所有员工信息。显示员工姓名，工资，职务名称，职务描述，部门名称，部门位置，工资等级。</p>
<figure>
<img data-src="img/image-20200727204121609.png" alt="image-20200727204121609">
<figcaption aria-hidden="true">image-20200727204121609</figcaption>
</figure>
<p>具体操作： 1. 确定要查询哪些表，emp e, job j, dept d, salarygrade
s</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询11-1597203787781.png"> <img data-src="img/多表查询12-1597203787781.png"></p>
<pre><code>  2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id and e.salary between s.losalary and s.hisalary</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id <span class="keyword">AND</span> e.salary <span class="keyword">BETWEEN</span> s.losalary <span class="keyword">AND</span> s.hisalary;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询13-1597203787781.png"> <img data-src="img/多表查询14-1597203787781.png"></p>
<pre><code>  3. 确定查询字段：员工姓名，工资，职务名称，职务描述，部门名称，部门位置，工资等级</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.ename, e.salary, j.jname, j.description, d.dname, d.loc, s.grade <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id <span class="keyword">AND</span> e.salary <span class="keyword">BETWEEN</span> s.losalary <span class="keyword">AND</span> s.hisalary;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询15-1597203787781.png"></p>
<h5 id="多表查询规律总结">1.3.1 多表查询规律总结</h5>
<ol type="1">
<li>不管我们查询几张表，表连接查询会产出笛卡尔积，我们需要消除笛卡尔积，拿到正确的数据。我们需要找到表与表之间通过哪个字段关联起来的（通常是<code>外键=主键</code>）</li>
<li>消除笛卡尔积规律：2张表需要1个条件，3张表需要2个条件，4张表需要3个条件。（条件数量=表的数量-1），每张表都要参与进来</li>
<li>多表连接查询步骤： 3.1. 确定要查询哪些表 3.2. 确定表连接条件 3.3.
确定查询字段</li>
</ol>
<h4 id="练习4-1">1.4 练习4</h4>
<p>查询经理的信息。显示员工姓名，工资，职务名称，职务描述，部门名称，部门位置，工资等级</p>
<p>具体操作： 1. 确定要查询哪些表，emp e, job j, dept d, salarygrade
s</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询11-1597203787781.png"> <img data-src="img/多表查询12-1597203787781.png"></p>
<pre><code>  2. 确定表连接条件 e.job_id=j.id and e.dept_id=d.id and e.salary between s.losalary and s.hisalary</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id <span class="keyword">AND</span> e.salary <span class="keyword">BETWEEN</span> s.losalary <span class="keyword">AND</span> s.hisalary;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询13-1597203787781.png"> <img data-src="img/多表查询14-1597203787781.png"></p>
<p>额外条件：只需要查询经理的信息（j.jname='经理'） <img data-src="img/多表查询16-1597203787782.png"></p>
<pre><code>  3. 确定查询字段：员工姓名，工资，职务名称，职务描述，部门名称，部门位置，工资等级</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> e.ename, e.salary, j.jname, j.description, d.dname, d.loc, s.grade <span class="keyword">FROM</span> emp e <span class="keyword">INNER</span> <span class="keyword">JOIN</span> job j <span class="keyword">INNER</span> <span class="keyword">JOIN</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> salarygrade s <span class="keyword">ON</span> e.job_id<span class="operator">=</span>j.id <span class="keyword">AND</span> e.dept_id<span class="operator">=</span>d.id <span class="keyword">AND</span> e.salary <span class="keyword">BETWEEN</span> s.losalary <span class="keyword">AND</span> s.hisalary <span class="keyword">AND</span> j.jname<span class="operator">=</span><span class="string">&#x27;经理&#x27;</span>;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询17-1597203787782.png"></p>
<h4 id="练习5">1.5 练习5</h4>
<p><strong>查询出每个部门的部门编号、部门名称、部门位置、部门人数</strong></p>
<p>具体操作： 1. 去员工表中找到每个部门的人数和部门id</p>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> dept_id, <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> emp <span class="keyword">GROUP</span> <span class="keyword">BY</span> dept_id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询18-1597203787782.png"></p>
<pre><code>  2. 再和部门表连接查询</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> <span class="operator">*</span> <span class="keyword">FROM</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> (<span class="keyword">SELECT</span> dept_id, <span class="built_in">COUNT</span>(<span class="operator">*</span>) <span class="keyword">FROM</span> emp <span class="keyword">GROUP</span> <span class="keyword">BY</span> dept_id) e <span class="keyword">ON</span> e.dept_id<span class="operator">=</span>d.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询19-1597203787784.png"> <img data-src="img/多表查询20-1597203787784.png"></p>
<pre><code>  3. 显示对应的字段</code></pre>
<p><figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">SELECT</span> d.id, d.dname, d.loc, e.total 部门人数 <span class="keyword">FROM</span> dept d <span class="keyword">INNER</span> <span class="keyword">JOIN</span> (<span class="keyword">SELECT</span> dept_id, <span class="built_in">COUNT</span>(<span class="operator">*</span>) total <span class="keyword">FROM</span> emp <span class="keyword">GROUP</span> <span class="keyword">BY</span> dept_id) e <span class="keyword">ON</span> e.dept_id<span class="operator">=</span>d.id;</span><br></pre></td></tr></table></figure></p>
<p><img data-src="img/多表查询21-1597203787784.png"> 最终效果： <img data-src="img/多表查询22-1597203787784.png"></p>
<h2 id="学习总结">学习总结</h2>
<ol type="1">
<li><p>能够使用内连接进行多表查询</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">select 列名 from 左表,右表 where 条件</span><br><span class="line">select 列名 from 左表 inner join 右表 on 条件</span><br></pre></td></tr></table></figure></li>
<li><p>能够使用左外连接和右外连接进行多表查询</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select 列名 from 左表 left join 右表 on 条件</span><br></pre></td></tr></table></figure>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">select 列名 from 左表 right join 右表 on 条件</span><br></pre></td></tr></table></figure></li>
<li><p>能够使用子查询进行多表查询</p>
<ol type="1">
<li>单行单列：父查询使用比较运算符</li>
<li>多行单列：父查询使用in</li>
<li>多行多列：将查询结果做成一张虚拟表，再次查询</li>
</ol></li>
<li><p>能够理解多表查询的规律</p>
<figure>
<img data-src="img/1563360456604.png" alt="1563360456604">
<figcaption aria-hidden="true">1563360456604</figcaption>
</figure></li>
</ol>
<h1 id="第三部分事务">第三部分(事务)</h1>
<h2 id="事务的概念">1、事务的概念</h2>
<h3 id="目标-19">目标</h3>
<p>能够理解事务的概念</p>
<h3 id="讲解-19">讲解</h3>
<h4 id="事务的应用场景说明">事务的应用场景说明</h4>
<p>关于事务在实际中的应用场景：</p>
<p>假设我在淘宝买了一部手机，然后当我付完款，钱已经从我的账户中扣除。正当此时，淘宝转账系统宕机了，那么此时淘宝还没有收到钱，而我的账户的钱已经减少了，这样就会导致我作为买家钱已经付过，而卖家还没有收到钱，他们不会发货物给我。这样做显然是不合理。实际生活中是如果淘宝出问题，作为用户的账户中钱是不应该减少的。这样用户就不会损失钱。</p>
<p>还有种情况，就是当我付完款之后，卖家看到我付款成功，然后直接发货了，我如果有权限操作，我可以撤销，这样就会导致我的钱没有减少，但是卖家已经发货，同样这种问题在实际生活中也是不允许出现的。</p>
<p>关于上述两种情况，使用数据库中的事务可以解决。具体解决方案如下图所示：</p>
<p><img data-src="img/mysql事务1.bmp"></p>
<p><strong>说明：在数据库中查询不会涉及到使用事务，都是增删改。</strong></p>
<h4 id="什么是事务">什么是事务</h4>
<p>​
在实际的业务开发中，有些业务操作要多次访问数据库。一个业务要发送多条SQL语句给数据库执行。需要将多次访问数据库的操作视为一个整体来执行，要么所有的SQL语句全部执行成功。如果其中有一条SQL语句失败，就进行事务的回滚，所有的SQL语句全部执行失败。</p>
<p>简而言之，事务指的是逻辑上的一组操作,组成这组操作的各个单元要么全都成功,要么全都失败。</p>
<p>事务作用：保证在一个事务中多次操作数据库表中数据时，要么全都成功,要么全都失败。</p>
<h3 id="小结-15">小结</h3>
<p>什么是事务？多条SQL组合再一起完成某个功能.</p>
<h2 id="手动提交事务">2、手动提交事务</h2>
<h3 id="目标-20">目标</h3>
<p>能够使用手动的方式提交事务</p>
<h3 id="讲解-20">讲解</h3>
<p>MYSQL中可以有两种方式进行事务的操作：</p>
<ol type="1">
<li>手动提交事务：先开启，再提交</li>
<li>自动提交事务(默认的):即执行一条sql语句提交一次事务。</li>
</ol>
<p>事务有关的SQL语句：</p>
<table>
<thead>
<tr class="header">
<th>SQL语句</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>start transaction;</td>
<td>开启手动控制事务</td>
</tr>
<tr class="even">
<td>commit;</td>
<td>提交事务</td>
</tr>
<tr class="odd">
<td>rollback;</td>
<td>回滚事务</td>
</tr>
</tbody>
</table>
<h4 id="手动提交事务使用步骤">手动提交事务使用步骤</h4>
<p>​ 第1种情况：开启事务 -&gt; 执行SQL语句 -&gt; 成功 -&gt; 提交事务 ​
第2种情况：开启事务 -&gt; 执行SQL语句 -&gt; 失败 -&gt; 回滚事务 <img data-src="img/事务01.png"></p>
<p>准备数据:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"># 创建一个表：账户表.</span><br><span class="line"><span class="keyword">create</span> database day04_db; </span><br><span class="line"># 使用数据库</span><br><span class="line">use day04_db;</span><br><span class="line"># 创建账号表</span><br><span class="line"><span class="keyword">create</span> <span class="keyword">table</span> account(</span><br><span class="line">	id <span class="type">int</span> <span class="keyword">primary</span> key auto_increment,</span><br><span class="line">	name <span class="type">varchar</span>(<span class="number">20</span>),</span><br><span class="line">	money <span class="keyword">double</span></span><br><span class="line">);</span><br><span class="line"># 初始化数据</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> account <span class="keyword">values</span> (<span class="keyword">null</span>,<span class="string">&#x27;a&#x27;</span>,<span class="number">1000</span>);</span><br><span class="line"><span class="keyword">insert</span> <span class="keyword">into</span> account <span class="keyword">values</span> (<span class="keyword">null</span>,<span class="string">&#x27;b&#x27;</span>,<span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
<p><strong>案例演示1</strong>：需求：演示提交事务，a给b转账100元。</p>
<p><img data-src="img/提交事务.bmp"></p>
<hr>
<p><strong>案例演示2</strong>：演示回滚事务，a给b转账100元。（失败）</p>
<p><img data-src="img/回滚事务.bmp"></p>
<p>注意：</p>
<p>事务是如何处理正常情况的呢？</p>
<p>a=1000 b=1000</p>
<p>开启事务(start transaction;)</p>
<p>update account set money = money -100 where name='a';</p>
<p>update account set money = money +100 where name='b';</p>
<p>提交事务(commit;)
(事务提交之后，sql语句对数据库产生的操作才会被永久的保存)</p>
<p>事务是如何处理异常情况的呢？</p>
<p>a=1000 b=1000</p>
<p>开启事务(start transaction;)</p>
<p>update t_account set money = money -100 where name='a'; a=900</p>
<p>出现异常</p>
<p>update t_account set money = money +100 where name='b';</p>
<p>事务的回滚(rollback;)(撤销已经成功执行的sql语句，回到开启事务之前的状态)</p>
<p>a=1000 b=1000;</p>
<p>注意：只要提交事务，那么数据就会长久保存了，就不能回滚事务了。即提交或者回滚事务都是代表结束当前事务的操作。</p>
<h3 id="小结-16">小结</h3>
<ol type="1">
<li>如何开启事务: start transaction;</li>
<li>如何提交事务: commit; 永久性改变</li>
<li>如何回滚事务: rollback; 回到开启事务最初的状态</li>
</ol>
<h2 id="自动提交事务">3、自动提交事务</h2>
<h3 id="目标-21">目标</h3>
<p>了解自动提交事务</p>
<p>能够关闭自动提交事务</p>
<h3 id="讲解-21">讲解</h3>
<p>​
MySQL的每一条DML(增删改)语句都是一个单独的事务，每条语句都会自动开启一个事务，执行完毕自动提交事务，MySQL默认开始自动提交事务。自动提交，通过修改mysql全局变量“autocommit”进行控制。</p>
<p>1.通过以下命令可以查看当前autocommit模式：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">&#x27;%commit%&#x27;</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/自动提交事务.bmp"></p>
<p>2.设置自动提交的参数为OFF:</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> autocommit <span class="operator">=</span> <span class="number">0</span>;  <span class="comment">-- 0:OFF  1:ON</span></span><br></pre></td></tr></table></figure>
<h3 id="小结-17">小结</h3>
<p>1）MySql默认自动提交。即执行一条sql语句提交一次事务。</p>
<p>2）设置autocommit为off状态，只是临时性的，下次重新连接mysql，autocommit依然变为on状态。</p>
<p>3）如果设置autocommit为off状态，那么当我们执行一条sql语句，就不会自动提交事务，重新启动可视化工具，数据并没有改变。</p>
<p>4）如果设置autocommit为on状态，如果我们先执行 start transaction;
然后在执行修改数据库的语句：</p>
<p>update account set money = money-100 where name='a';</p>
<p>update account set money = money+100 where name='b';</p>
<p>那么此时就表示上述修改数据库的sql语句都在同一个事务中，此时必须手动提交事务，即commit;</p>
<p>换句话说，如果我们手动开启事务 start transaction;
那么此时mysql就不会自动提交事务，必须手动提交事务。</p>
<p>5）如果设置autocommit为on状态，如果我们不执行 start transaction;
直接执行修改数据库的语句：</p>
<p>update account set money = money-100 where name='a';</p>
<p>update account set money = money+100 where name='b';</p>
<p>那么此时mysql就会自动提交事务。即上述每条sql语句就是一个事务。</p>
<p>课堂代码演示：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 自动提交事务：每条sql语句就是一个事务，那么执行一条sql语句就会提交一次事务</span></span><br><span class="line"><span class="comment">-- mysql数据库就是自动提交事务</span></span><br><span class="line"><span class="comment">-- a给b转账100元</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="number">-100</span> <span class="keyword">where</span> name<span class="operator">=</span><span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="operator">+</span><span class="number">100</span> <span class="keyword">where</span> name<span class="operator">=</span><span class="string">&#x27;b&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">-- 查看mysql是否自动提交事务</span></span><br><span class="line"><span class="comment">-- autocommit的值是on表示自动提交事务，值是off表示关闭自动提交事务</span></span><br><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">&#x27;%commit%&#x27;</span>;</span><br><span class="line"><span class="comment">-- 我们可以使用命令临时设置mysql变为手动提交事务，即将自动提交事务关闭</span></span><br><span class="line"><span class="comment">-- 下次重新连接mysql依然是自动提交事务</span></span><br><span class="line"><span class="keyword">set</span> autocommit <span class="operator">=</span> <span class="number">0</span>; <span class="comment">-- 0 表示关闭自动提交事务 1表示开启自动事务</span></span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="number">-100</span> <span class="keyword">where</span> name<span class="operator">=</span><span class="string">&#x27;a&#x27;</span></span><br></pre></td></tr></table></figure>
<h2 id="原理">4、原理</h2>
<figure>
<img data-src="img/事务19.png" alt="1562228567293">
<figcaption aria-hidden="true">1562228567293</figcaption>
</figure>
<h3 id="原理说明">原理说明</h3>
<ol type="1">
<li>一个用户登录成功以后，服务器会创建一个临时日志文件。日志文件用来保存用户事务状态。</li>
<li>如果没有使用事务，则所有的操作直接写到数据库中，不会使用日志文件。</li>
<li>如果开启事务，将所有的写操作写到日志文件中。</li>
<li>如果这时用户提交了事务，则将日志文件中所有的操作写到数据库中。</li>
<li>如果用户回滚事务，则日志文件会被清空，不会影响到数据库的操作。</li>
</ol>
<h3 id="事务操作小结">事务操作小结</h3>
<p>说出事务原理？
开启事务后,SQL语句会放在临时的日志文件中,如果提交事务,将日志文件中SQL的结果放在数据库中</p>
<p>如果回滚事务清空日志文件.</p>
<table>
<thead>
<tr class="header">
<th><strong>事务的操作</strong></th>
<th><strong>MySQL操作事务的语句</strong></th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td><strong>手动开启事务</strong></td>
<td>start transaction</td>
</tr>
<tr class="even">
<td><strong>手动提交事务</strong></td>
<td>commit</td>
</tr>
<tr class="odd">
<td><strong>手动回滚事务</strong></td>
<td>rollback</td>
</tr>
<tr class="even">
<td><strong>查询事务的自动提交情况</strong></td>
<td>show variables like '%commit%';</td>
</tr>
<tr class="odd">
<td><strong>设置事务的手动提交方式</strong></td>
<td>set autocommit = 0 -- 关闭自动提交</td>
</tr>
</tbody>
</table>
<h2 id="事务的四大特性acid面试">5、事务的四大特性(ACID)(面试)</h2>
<h3 id="目标-22">目标</h3>
<p>了解事务的四大特性</p>
<h3 id="讲解-22">讲解</h3>
<p>数据库的事务必须具备ACID特性，ACID是指
Atomicity（原子性）、Consistensy（一致性）、Isolation（隔离性）和Durability（持久性）的英文缩写。</p>
<p><strong>1、隔离性（Isolation）</strong></p>
<p>多个用户并发的访问数据库时，一个用户的事务不能被其他用户的事务干扰，多个并发的事务之间要相互隔离。</p>
<p>一个事务的成功或者失败对于其他的事务是没有影响。2个事务应该相互独立。</p>
<p>举例：</p>
<p>a 给b转账 -----》叫做事务A</p>
<p>c 给d 转账 -----》叫做事务B</p>
<p>事务A和事务B之间不会相互影响。</p>
<p><strong>2、持久性（Durability）</strong></p>
<p>指一个事务一旦被提交，它对数据库的改变将是永久性的，哪怕数据库发生异常，重启之后数据亦然存在。</p>
<p>举例：</p>
<p>a=1000、b=1000转账</p>
<p>开启事务</p>
<p>a-100</p>
<p>b+100</p>
<p>提交</p>
<p>结果： a 900 b 1100</p>
<p>即使事务提交以后再发生异常，a和b的数据依然不会变。a就是900
b就是1100。</p>
<p><strong>3、原子性（Atomicity）</strong></p>
<p><strong>原子性是指事务</strong>包装的一组sql(一组业务逻辑)是一个不可分割的工作单位，事务中的操作要么都发生，要么都不发生。</p>
<p><strong>4、一致性（Consistency）</strong></p>
<p>一个事务在执行之前和执行之后 数据库都必须处于一致性状态。</p>
<p>如果事务成功的完成，那么数据库的所有变化将生效。</p>
<p>如果事务执行出现错误，那么数据库的所有变化将会被回滚（撤销），返回到原始状态。</p>
<p><strong>事务的成功与失败，最终数据库的数据都是符合实际生活的业务逻辑。一致性绝大多数依赖业务逻辑和原子性。</strong></p>
<p>举例1： a=1000、b=1000 转账 100</p>
<p>a - 100</p>
<p>b + 100</p>
<p>结果： a + b = 2000</p>
<p>如果a转账失败了，那么b也得失败。不能因为a失败了，a依然是1000.但是b却成功了，b却变成了1100.那么结果是2100了，这样是不符合事务的一致性的。</p>
<h3 id="小结-18">小结</h3>
<p>事务四个特性？ 原子性 一致性 隔离性 持久性</p>
<table>
<colgroup>
<col style="width: 25%">
<col style="width: 74%">
</colgroup>
<thead>
<tr class="header">
<th>事务特性</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>一致性（Consistency）</td>
<td>事务前后数据的完整性必须保持一致</td>
</tr>
<tr class="even">
<td>原子性（Atomicity）</td>
<td>事务是一个不可分割的工作单位，事务中的操作要么都发生，要么都不发生。</td>
</tr>
<tr class="odd">
<td>隔离性（Isolation）</td>
<td>是指多个用户并发访问数据库时，一个用户的事务不能被其它用户的事务所干扰，多个并发事务之间数据要相互隔离，不能相互影响。</td>
</tr>
<tr class="even">
<td>持久性（Durability）</td>
<td>指一个事务一旦被提交，它对数据库中数据的改变就是永久性的，接下来即使数据库发生故障也不应该对其有任何影响</td>
</tr>
</tbody>
</table>
<h2 id="事务的并发访问引发的三个问题面试">6、事务的并发访问引发的三个问题(面试)</h2>
<h3 id="目标-23">目标</h3>
<p>能够理解并发访问的三个问题</p>
<h3 id="讲解-23">讲解</h3>
<p>事务在操作时的理想状态：多个事务之间互不影响，如果隔离级别设置不当就可能引发并发访问问题。</p>
<table>
<colgroup>
<col style="width: 18%">
<col style="width: 81%">
</colgroup>
<thead>
<tr class="header">
<th>并发访问的问题</th>
<th>含义</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>脏读</td>
<td>一个事务读取到了另一个事务中尚<strong>未提交的数据</strong>。最严重，杜绝发生。</td>
</tr>
<tr class="even">
<td>不可重复读</td>
<td>一个事务中两次读取的<strong>数据内容</strong>不一致，要求的是一个事务中多次读取时数据是不一致的，这是事务<strong>update时</strong>引发的问题</td>
</tr>
<tr class="odd">
<td>幻读(虚读)</td>
<td>一个事务内读取到了别的<strong>事务插入或者删除的数据</strong>，导致前后读取记录行数不同。这是<strong>insert或delete</strong>时引发的问题</td>
</tr>
</tbody>
</table>
<p><strong>1.脏读：指一个事务读取了另外一个事务未提交的数据。(非常危险)</strong></p>
<p>​ 脏读具体解释如下图所示：注意脏读的前提是没有事务的隔离性。</p>
<p><img data-src="/Users/ethan.liu/Desktop/02_CodeData/Code#016%2003-数据库阶段课件/数据库_day02/01_课件/授课笔记/img/脏读.bmp"></p>
<p>说明：事务a首先执行转账操作，然后事务a还没有提交数据的情况下，事务b读取了数据库的数据。紧接着事务a执行回滚操作，导致事务b读取的结果和数据库的实际数据是不一样的。</p>
<p>一个事务读取了另一个事务未提交的数据叫做脏读。</p>
<p>举例：</p>
<p>a 转账 给b 100，未提交</p>
<p>b 查询账户多了100</p>
<p>a 回滚</p>
<p>b 查询账户那100不见了。</p>
<p>一个事务读取了另一个事务没有提交的数据，非常严重，必须避免脏读。</p>
<p><strong>2.不可重复读：在一个事务内多次读取表中的数据，多次读取的结果不同。</strong></p>
<p><img data-src="img/不可重复读.bmp"></p>
<p>说明：事务b首先读取数据库的数据，然后事务a对数据修改并提交。之后事务b对数据库再次进行读取。这时发现在事务b中2次读取的结果不一致。</p>
<p>一个事务内读取了另一个事务提交的数据。这个叫做不可重复读。</p>
<p>不可重复读和脏读的区别：</p>
<p>脏读：强调的是读取了未提交的数据。</p>
<p>不可重复读：一个事务内2次读取，其中一次读取了另一个事务提交了的数据。</p>
<p>例如:
银行想查询A账户的余额，第一次查询的结果是200元，A向账户中又存了100元。此时，银行再次查询的结果变成了300元。两次查询的结果不一致，银行就会很困惑，以哪次为准。</p>
<p>和脏读不同的是：脏读读取的是前一事务未提交的数据，不可重复度
读取的是前一事务已提交的事务。</p>
<p>很多人认为这有啥好困惑的，肯定是以后面的结果为准了。我们需要考虑这样一种情况，查询A账户的余额，一个打印到控制台，一个输出到硬盘上，同一个事务中只是顺序不同，两次查询结果不一致，到底以哪个为准，你会不会困惑呢？</p>
<p>当前事务查询A账户的余额为100元，另外一个事务更新余额为300元并提交，导致当前事务使用同一查询结果却变成了300元。</p>
<p><strong>3.幻读（虚读）:一个事务内读取到了别的事务插入或者删除的数据，导致前后读取记录行数不同</strong></p>
<p><img data-src="img/幻读.bmp"></p>
<p>说明：事务b首先读取数据的数量，然后事务a添加了一条数据，并且提交了。接着事务b再次读取了数据的数量。2次读取不一致。</p>
<p>同一个事务内，2次读取的数据的数量不一致，叫做幻读或者虚读。</p>
<p>虚读(幻读)和不可重复读的区别：</p>
<p>不可重复读：强调的是数据内容的不一致。另一个事务是update操作。</p>
<p>虚读(幻读)：强调的数据的数量(记录数)的不一致。另一个事务是insert或者delete操作。</p>
<p>注意：</p>
<p>指在一个事务中 读取 另一个事务 插入或删除
数据记录，导致当前事务读取数据的记录数前后不一致。</p>
<p>一个事务读取另一个事务已经提交的数据，强调的是记录数的变化，常用sql类型为
insert和 delete。</p>
<h3 id="小结-19">小结</h3>
<ol type="1">
<li>能够理解并发访问的三个问题
赃读:一个事务读取另一个事务还没有提交的数据,一定避免。
不可重复读:一个事务读取多次数据内容不一样，主要是update语句。事务已经提交了。
可以发生的。
幻读:一个事务读取多次数量不一样，主要是delete或者insert语句。事务已经提交了。可以发生的。</li>
</ol>
<h2 id="事务的隔离级别">7、事务的隔离级别</h2>
<h3 id="目标-24">目标</h3>
<p>能够说出mysql的四种隔离级别</p>
<h3 id="讲解-24">讲解</h3>
<p>1、通过以上问题演示，我们发现如果不考虑事务的隔离性，会遇到脏读、不可重复读和虚读等问题。所以在数据库中我们要对上述三种问题进行解决。MySQL数据库规范规定了4种隔离级别，分别用于描述两个事务并发的所有情况。</p>
<p>上面的级别最低，下面的级别最高。“是”表示会出现这种问题，“否”表示不会出现这种问题。</p>
<table>
<colgroup>
<col style="width: 6%">
<col style="width: 12%">
<col style="width: 25%">
<col style="width: 6%">
<col style="width: 15%">
<col style="width: 6%">
<col style="width: 28%">
</colgroup>
<thead>
<tr class="header">
<th>级别</th>
<th>名字</th>
<th>隔离级别</th>
<th>脏读</th>
<th>不可重复读</th>
<th>幻读</th>
<th>数据库默认隔离级别</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>1</td>
<td>读未提交</td>
<td>read uncommitted</td>
<td>是</td>
<td>是</td>
<td>是</td>
<td></td>
</tr>
<tr class="even">
<td>2</td>
<td>读已提交</td>
<td>read committed</td>
<td>否</td>
<td>是</td>
<td>是</td>
<td>Oracle和SQL Server</td>
</tr>
<tr class="odd">
<td>3</td>
<td>可重复读</td>
<td>repeatable read</td>
<td>否</td>
<td>否</td>
<td>是</td>
<td>MySQL</td>
</tr>
<tr class="even">
<td>4</td>
<td>串行化</td>
<td>serializable</td>
<td>否</td>
<td>否</td>
<td>否</td>
<td></td>
</tr>
</tbody>
</table>
<p>2、安全和性能对比</p>
<p>安全性：serializable &gt; repeatable read &gt; read committed &gt;
read uncommitted</p>
<p>性能 ： serializable &lt; repeatable read &lt; read committed &lt;
read uncommitted</p>
<p>3、注意：其实三个问题，开发中最严重的问题就是脏读，这个问题一定要避免，而关于不可重复读和虚读其实只是感官上的错误，并不是逻辑上的错误。就是数据的时效性，所以这种问题并不属于很严重的错误。如果对于数据的时效性要求不是很高的情况下，我们是可以接受不可重复读和虚读的情况发生的。</p>
<h3 id="小结-20">小结</h3>
<p>能够说出mysql的四种隔离级别 读未提交:read uncommitted
<strong>读已提交:read committed 可以避免脏读</strong>
<strong>可重复读:repeatable read mysql默认的，可以避免脏读
和不可重复读</strong> 串行化:serializable</p>
<h2 id="脏读的演示课下不用演示">8、脏读的演示(课下不用演示)</h2>
<h3 id="目标-25">目标</h3>
<ol type="1">
<li>能够设置mysql的隔离级别</li>
<li>能够解决赃读</li>
</ol>
<h3 id="讲解-25">讲解</h3>
<h4 id="查询和设置隔离级别">查询和设置隔离级别</h4>
<ol type="1">
<li><p>查询全局事务隔离级别</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">show</span> variables <span class="keyword">like</span> <span class="string">&#x27;%isolation%&#x27;</span>;</span><br><span class="line"><span class="comment">-- 或</span></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@tx_isolation</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/事务27.png"></p></li>
<li><p>设置事务隔离级别，需要退出MSQL再进入MYSQL才能看到隔离级别的变化</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level 隔离级别;</span><br><span class="line"><span class="comment">-- 如:</span></span><br><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level read uncommitted;</span><br><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level repeatable read;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/事务28.png"></p></li>
</ol>
<p>3.注意，mysql5.6后如果将隔离级别设置uncommitted，那么修改数据会报错，</p>
<figure>
<img data-src="img/image-20210717105819743.png" alt="image-20210717105819743">
<figcaption aria-hidden="true">image-20210717105819743</figcaption>
</figure>
<p>所以我们在演示的时候，针对当前会话设置如下：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"># 设置session级别的BINLOG  </span><br><span class="line"><span class="keyword">SET</span> SESSION binlog_format <span class="operator">=</span> <span class="string">&#x27;MIXED&#x27;</span> ;</span><br></pre></td></tr></table></figure>
<p>上述命令只是当前会话有效，重新连接失效，通过如下命令可以查看：</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#查看binlog方式      </span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@binlog_format</span>;</span><br></pre></td></tr></table></figure>
<h4 id="脏读的演示">脏读的演示</h4>
<p>脏读：一个事务读取到了另一个事务中尚未提交的数据。</p>
<ol type="1">
<li><p>打开一个窗口,设置为A窗口，登录MySQL，设置全局的隔离级别为最低</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 设置窗口名字A</span></span><br><span class="line">title A</span><br><span class="line"><span class="comment">-- 登录mysql数据库</span></span><br><span class="line">mysql <span class="operator">-</span>u root <span class="operator">-</span>p</span><br><span class="line"><span class="number">1234</span></span><br><span class="line"><span class="comment">-- 设置事务隔离级别</span></span><br><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level read uncommitted;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/事务290.bmp"></p></li>
</ol>
<p><strong>注意：设置事务隔离级别，需要重新打开一个窗口才能看到隔离级别的变化.</strong></p>
<p>2.重新打开一个新的窗口,设置为B窗口，登录mysql</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">-- 设置窗口名字B</span></span><br><span class="line">title B;</span><br><span class="line"><span class="comment">-- 登录mysql数据库</span></span><br><span class="line">mysql <span class="operator">-</span>u root <span class="operator">-</span>p</span><br><span class="line"><span class="number">1234</span></span><br><span class="line"><span class="comment">-- 查询隔离级别</span></span><br><span class="line"><span class="keyword">select</span> @<span class="variable">@tx_isolation</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/294.bmp"></p>
<p>3.AB窗口都开启事务</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">use day05_db;</span><br><span class="line"><span class="keyword">start</span> transaction;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/293.bmp"></p>
<p>4.A窗口更新2个人的账户数据，未提交</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="number">-500</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">1</span>;</span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="operator">+</span><span class="number">500</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">2</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/295.bmp"></p>
<p>5.B窗口查询账户</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">select</span> <span class="operator">*</span> <span class="keyword">from</span> account;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/事务32.png"></p>
<p>6.A窗口回滚</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">rollback</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/296.bmp"></p>
<p>7.B窗口查询账户，钱没了 <img data-src="img/事务34.png"></p>
<p>脏读非常危险的，比如张三向李四购买商品，张三开启事务，向李四账号转入500块，然后打电话给李四说钱已经转了。李四一查询钱到账了，发货给张三。张三收到货后回滚事务，李四的再查看钱没了。</p>
<p><strong>解决脏读的问题</strong>：将全局的隔离级别进行提升</p>
<ol type="1">
<li><p>在A窗口设置全局的隔离级别为<code>read committed</code></p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level read committed;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/事务35.png"></p></li>
<li><p>B窗口退出MySQL，B窗口再进入MySQL <img data-src="img/事务36.png"></p></li>
<li><p>AB窗口同时开启事务 <img data-src="img/事务37.png"></p></li>
<li><p>A更新2个人的账户，未提交</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="number">-500</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">1</span>;</span><br><span class="line"><span class="keyword">update</span> account <span class="keyword">set</span> money<span class="operator">=</span>money<span class="operator">+</span><span class="number">500</span> <span class="keyword">where</span> id<span class="operator">=</span><span class="number">2</span>;</span><br></pre></td></tr></table></figure>
<p><img data-src="img/297.bmp"></p></li>
<li><p>B窗口查询账户 <img data-src="img/298.bmp"></p></li>
<li><p>A窗口commit提交事务 <img data-src="img/事务40.png"></p></li>
<li><p>B窗口查看账户 <img data-src="img/事务41.png"></p></li>
</ol>
<blockquote>
<p>结论：read committed的方式可以避免脏读的发生</p>
</blockquote>
<h3 id="小结-21">小结</h3>
<ol type="1">
<li><p>查询全局事务隔离级别？ show variables like '%isolation%'; select
@<span class="citation" data-cites="tx_isolation">@tx_isolation</span>;</p></li>
<li><p>设置全局事务隔离级别？ set global transaction isolation level
隔离级别字符串;</p>
<figure class="highlight sql"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">set</span> <span class="keyword">global</span> transaction isolation level repeatable read;</span><br></pre></td></tr></table></figure></li>
<li><p>如何解决赃读? 将隔离级别设置为:read committed</p></li>
</ol>
</div></div></div></div>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/" title="MYSQL基础语法（下）">http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL高级笔记/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/MySQL/" rel="tag"><i class="fa fa-tag"></i> MySQL</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/04/30/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day11/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day10-11-%E9%9A%8F%E5%A0%82%E5%B0%8F%E8%AE%B0/" rel="prev" title="JavaSE进阶——Day10-11 随堂小记">
                  <i class="fa fa-angle-left"></i> JavaSE进阶——Day10-11 随堂小记
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/05/02/JavaNotes/JavaSE%E8%BF%9B%E9%98%B6/day12/JavaSE%E8%BF%9B%E9%98%B6%E2%80%94%E2%80%94Day12-%E7%BD%91%E7%BB%9C%E7%BC%96%E7%A8%8B/" rel="next" title="JavaSE进阶——Day12 网络编程">
                  JavaSE进阶——Day12 网络编程 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="efb054fbd33f314ba5060558e5ac1b0a"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/05/01/JavaNotes/MySQL/day02/day02-MySQL%E9%AB%98%E7%BA%A7%E7%AC%94%E8%AE%B0/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
